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 - Retired Efficiency! Implement Chooser UI

Weird Error

I dont know if anyone has noticed this but I am getting out of bounds exception error when I am in case statement and option I choose was choose then when I select index 1 throws an out of bound exception

ERROR

There are 0 songs available, your options are add - add a new song to the book choose - choose a song to sing quit - leave it :Exit the program
What do you want to doadd Enter the Artist NameRihanna Enter the TitleUmbrealla Enter the Video URLhttps://youtube.com Song: Umbrealla by Rihanna added !

There are 1 songs available, your options are add - add a new song to the book choose - choose a song to sing quit - leave it :Exit the program
What do you want to dochoose Available artist 1.) Rihanna 1 Your choiceException in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at model.KaraokeMachine.PromptArtist(KaraokeMachine.java:102) at model.KaraokeMachine.run(KaraokeMachine.java:65) at Karaoke.main(Karaoke.java:20)

can anyone help me here

package model;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; import java.util.ArrayList; import java.util.HashMap; import java.util.List;

public class KaraokeMachine {

private SongBook mSongBook;

private BufferedReader mReader;

private Map<String, String> mMenu;
// Lets give it songs

public  KaraokeMachine(SongBook songBook){

    mSongBook = songBook;

    mReader = new BufferedReader(new InputStreamReader(System.in));

    mMenu = new HashMap<String,String>();

    mMenu.put("add", " add  a new song to the book");
    mMenu.put("choose", "choose a song to sing");
    mMenu.put("quit", " leave it :Exit the program ");  
}

private String promptAction() throws IOException{

    System.out.printf(" There are %d songs available, your options are %n",mSongBook.getSongCount());


    for(Map.Entry<String,String> option: mMenu.entrySet()){
        System.out.printf("%s - %s %n" , option.getKey(), option.getValue());
    }

    System.out.print(" What do you want to do");

    String choice = mReader.readLine();

    return choice.trim().toLowerCase(); 
}

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); // This will invoke the To String method in the Song class
            break;

        case "choose":
            String artist = PromptArtist();
            Song artistSong = promptSongForArtist(artist);
            //TODO : Add to a Song Queue
            System.out.printf(" you cose %s%n", artistSong);

        case "quit":
            System.out.println(" Thanks for the choosing the songs");
            break;

            default:
                System.out.printf("Unknown choice : '%s' . Try again %n%n%n", choice);
        }
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
}

    while(!choice.equals("quit"));

}

//Code to Add The new song.

private Song promptNewSong() throws IOException{  // Return tyoe is Song bcz its returning to the class Song
    System.out.print(" Enter the Artist Name");
    String artist = mReader.readLine();
    System.out.print(" Enter the Title");
    String title = mReader.readLine();
    System.out.print(" Enter the Video URL");
    String URL = mReader.readLine();
    return new Song(artist,title,URL);
}

private String PromptArtist() throws IOException{
    System.out.println("Available artist");
    List<String> artists = new ArrayList<String>(mSongBook.getArtists());
    int index = PromptForIndex(artists);
    return artists.get(index);
}

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



private int PromptForIndex(List<String> songTitles) throws IOException{
    int count = 1;

    for(String option : songTitles){
        System.out.printf(" %d.) %s %n", count,option);
        count++;
    }

    String OptionAsString = mReader.readLine();
    int choice = Integer.parseInt(OptionAsString.trim());
    System.out.print(" Your choice");
    return count -1;


}

}

1 Answer

Rares Conea
PLUS
Rares Conea
Courses Plus Student 15,000 Points

Hi,

In private int promptForIndex you should return choice - 1 not count - 1, if you return count - 1 in your case of having one artist the method will return 1 and you only have one artist at position 0.