Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Efficiency! Implement Chooser UI

Amanda Lam
Amanda Lam
2,178 Points

problem when compiling

Hi,

When I try to run my code, I get two errors which I am confused by because I think that my code is exactly the same as Craig's:

./com/teamtreehouse/KaraokeMachine.java:55: error: incompatible types: String cannot b
e converted to Song                                                                   
          Song artistSong = promptSongForArtist(artist);                              
                                               ^                                      
./com/teamtreehouse/KaraokeMachine.java:94: error: incompatible types: Song cannot be 
converted to String                                                                   
    return songs.get(index);                                                          
                    ^ 

Here is my code:

public void run() {
    String choice = "";
    do {
      try {
      choice = promptAction();
      switch(choice) {
        case "add":
          Song song = promptNewSong();
          mSongBook.addSong(song);
          System.out.printf("%s added: %n%n", song);
          break;
        case "quit":
          System.out.println("Thanks for playing!");
          break;
        case "choose":
          String artist = promptArtist();
          Song artistSong = promptSongForArtist(artist);
          // TODO: Add to a song queue.
          System.out.printf("You chose: %s %n", artistSong);
          break;
        default:
          System.out.printf("Unknown choice: '%s'. Try Again. %n%n%n", choice);
      }
      } catch (IOException ioe) {
        System.out.println("Problem with input");
        ioe.printStackTrace();
      }
    } 
    while (!choice.equals("quit"));
  }
private String promptArtist() throws IOException {
    System.out.println("Available artists:");
    List<String> artists = new ArrayList<>(mSongBook.getArtists());
    int index = promptForIndex(artists);
    return artists.get(index);
  }

  private String promptSongForArtist(String artist) throws IOException {
    List<Song> songs = mSongBook.getSongsForArtist(artist);
    List<String> songTitles = new ArrayList<>();
    for (Song song : songs) {
      songTitles.add(song.getTitle());
    }
    int index = promptForIndex(songTitles);
    return songs.get(index);
  }

  private int promptForIndex(List<String> options) throws IOException {
    int counter = 1;
    for (String option : options) {
      System.out.printf("%d.)  %s %n", counter, option);
      counter++;
    }
    String optionAsString = mReader.readLine();
    int choice = Integer.parseInt(optionAsString.trim());
    System.out.print("Your choice: ");
    return choice - 1;
  }
Amanda Lam
Amanda Lam
2,178 Points

I don't know why, but some of my code won't format properly. Sorry about that.

1 Answer

deckey
deckey
14,630 Points

Hi Amanda, your variable artistSong is of type Song:

Song artistSong = promptSongForArtist(artist); 

but the used method promptSongForArtist(artist) is declared to return a String?

private String promptSongForArtist(String artist)

that should solve compile error(s)

As for the code not being shown nicely, try putting a blank line between your comments and the code, works better for me that way

good luck!

Amanda Lam
Amanda Lam
2,178 Points

Just figured out my mistake and changed String to Song for promptSongForArtist. Thank you very much :)