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

KaraokeMachine.java catch exceptions that are not included in the course

I found out that when selecting an artist or a song that is not listed causes an error and stops the program.

Example if we have artists 1-5 or song 1-3 for an artist, and selecting a number higher or a letter = crash

Haven't figured out how to catch the exceptions. Help would be grateful.

This is the code for artist/song options:

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 "choose":
                    String singerName = promptForSingerName();
                    String artist = promptArtist();
                    Song artistSong = promptSongForArtist(artist);
                    SongRequest songRequest = new SongRequest(singerName, artistSong);
                    if (mSongRequestQueue.contains(songRequest)) {
                        System.out.printf("%n%n Whoops %s already requested %s!%n",
                                singerName,
                                artistSong);
                        break;
                    }
                    mSongRequestQueue.add(songRequest);
                    System.out.printf("You chose: %s %n", artistSong);
                    break;
                case "play":
                    playNext();
                    break;
                case "quit":
                    System.out.println("Thanks for singing!");
                    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("\nAvailable artists, choose a number:");
    List<String> artists = new ArrayList<>(mSongBook.getArtists());
    int index = promptForIndex(artists);
    return artists.get(index);
}

private Song promptSongForArtist(String artist) throws IOException {
    List<Song> songs = mSongBook.getSongsForArtist(artist);
    List<String> songTitles = new ArrayList<>();
    for (Song song : songs) {
        songTitles.add(song.getTitle());
    }
    System.out.printf("\nAvailable songs for %s: %n", artist);
    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++;
    }
    System.out.print("Your choice:  ");
    String optionAsString = mReader.readLine();
    int choice = Integer.parseInt(optionAsString.trim());
    return choice - 1;
}

1 Answer

Have figured out that one needs to try and catch with IndexOutOfBoundsException, but can't figure out where one should put it.