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! Queueing

Sean Flanagan
Sean Flanagan
33,235 Points

Problem playing a song

Hi. My song won't play.

Here's my KaraokeMachine:

package com.teamtreehouse;

import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;

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

public class KaraokeMachine {
  private SongBook mSongBook;
  private BufferedReader mReader;
  private Queue<Song> mSongQueue;
  private Map<String, String> mMenu;

  public KaraokeMachine(SongBook songBook) {
    mSongBook = songBook;
    mReader = new BufferedReader(new InputStreamReader(System.in));
    mSongQueue = new ArrayDeque<Song>();
    mMenu = new HashMap<String, String>();
    mMenu.put("add", "Add a new song to the song book");
    mMenu.put("play", "Play the next song in the queue");
    mMenu.put("choose", "Choose a song to sing!");
    mMenu.put("quit", "Give up. Exit the program");
  }

  private String promptAction() throws IOException {
    System.out.printf("There are %d songs available and %d in the queue. Your options are: %n",
                      mSongBook.getSongCount(),
                      mSongQueue.size());
    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);
            break;
          case "choose":
            String artist = promptArtist();
            Song artistSong = promptSongForArtist(artist);
            mSongQueue.add(artistSong);
            System.out.printf("You chose: %s %n", artistSong);
            break;
          case "play":
            playNext();
            break;
          case "quit":
            System.out.println("Thanks for playing!");
            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 Song promptNewSong() throws IOException {
    System.out.print("Enter the artist's name: ");
    String artist = mReader.readLine();
    System.out.print("Enter the title: ");
    String title = mReader.readLine();
    System.out.print("Enter the video URL: ");
    String videoUrl = mReader.readLine();
    return new Song(artist, title, videoUrl);
  }

  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 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("Available 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;
  }

  public void playNext() {
    Song song = mSongQueue.poll();
    if (song == null) {
      System.out.println("Sorry there are no songs in the queue. " + " Use choose from the menu to         add some");
    } else {
      System.out.printf("%n%n%n Open %s to hear %s by %s %n%n%n",
                        song.getVideoUrl(),
                        song.getTitle(),
                        song.getArtist());
    }
  }
}

Errors:

Enter the artist's name: The Eagles                                                              
Enter the title: Hollywood Waltz                                                                 
Enter the video URL: https://www.youtube.com/watch?v=bsZCXx5WU3s                                 
Song:  Hollywood Waltz by The Eagles added!                                                      

There are 1 songs available and 0 in the queue. Your options are:                                
add - Add a new song to the song book                                                            
play - Play the next song in the queue                                                           
choose - Choose a song to sing!                                                                  
quit - Give up. Exit the program                                                                 
What do you want to do: choose                                                                   
Available artists:                                                                               
1.) The Eagles                                                                                   
Your choice: 1                                                                                   
Available songs for The Eagles:                                                                  
1.) Hollywood Waltz                                                                              
Your choice: play                                                                                
Exception in thread "main" java.lang.NumberFormatException: For input string: "play"             
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)         
        at java.lang.Integer.parseInt(Integer.java:580)                                          
        at java.lang.Integer.parseInt(Integer.java:615)                                          
        at com.teamtreehouse.KaraokeMachine.promptForIndex(KaraokeMachine.java:118)              
        at com.teamtreehouse.KaraokeMachine.promptSongForArtist(KaraokeMachine.java:106)         
        at com.teamtreehouse.KaraokeMachine.run(KaraokeMachine.java:61)                          
        at Karaoke.main(Karaoke.java:10)

Does anyone know why the song won't play? Cheers :-)

Snapshot: https://w.trhou.se/bapbpc88kw

1 Answer

Enrique Munguía
Enrique Munguía
14,311 Points

When you make your choice you must enter a number because the method promptForIndex expects an integer and you are entering a string "play" and the method throws NumberFormatException as it can not parse the String to Integer

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Enrique. It looks like I entered a string instead of a number so you're right. I typed 'play' too early. When asked what I wanted to do, <i>then</i> I typed 'play'. Thanks for your help. :-)