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 trialAndrea Miotto
iOS Development Techdegree Graduate 23,357 PointsMake a choose before an add
As the title, if you try to make a choose before adding any song, we get and error. This is an exception that we did not manage.
What do u want to do: choose
Available artists:
Your choice:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at com.teamtreehouse.KaraokeMachine.promptForIndex(KaraokeMachine.java:113)
at com.teamtreehouse.KaraokeMachine.promptArtist(KaraokeMachine.java:90)
at com.teamtreehouse.KaraokeMachine.run(KaraokeMachine.java:59)
at Karaoke.main(Karaoke.java:9) ```
3 Answers
Andrea Miotto
iOS Development Techdegree Graduate 23,357 PointsI just changed KaraokeMachine.java, the run() method:
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 "play":
playNext();
break;
case "choose":
//added an if and an else. Probably there is a cleaner way to do it with IOException
if (mSongBook.getSongCount() != 0) {
String artist = promptArtist();
Song artistSong = promptSongForArtist(artist);
mSongQueue.add(artistSong);
System.out.printf("You chose: %s %n", artistSong);
break;
} else {
System.out.println("\nThere are no songs availabe. Make a new choise \n");
break;
}
case "quit":
System.out.println("Thanks for playing!");
break;
default:
System.out.printf("Unknow choice: '%s'. Try again. %n%n%n", choice);
}
} catch (IOException ioe) {
System.out.println("Problem with input");
ioe.printStackTrace();
}
} while (!choice.equals("quit"));
}
Iskandar Fendi
5,700 PointsCan you show me your code in line 65?
Andrea Miotto
iOS Development Techdegree Graduate 23,357 PointsThank you Iskandar, but I know how to fix it, I was just warning Craig Dannis about that.
Craig Dennis
Treehouse TeacherGood call! Do you have code to fix it?
This should definitely be part of a unit test!
Thanks!