Java Java Data Structures Efficiency! Menu UI

Phoenix S.
Phoenix S.
2,456 Points

I keep getting an error even though my code is the exact same

Here's my Code for my files from the video. It keeps failing with the error message: error: unreported exception IOException; must be caught or declared to be thrown

KaraokeMachine machine = new KaraokeMachine(songBook);                                                                                                                    

1 error

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

public class Karaoke {
  public static void main(String[] args) {
//    Song song = new Song(
//      "Michael Jackson", 
//      "Beat It",
//      "");
    SongBook songBook = new SongBook();
//    System.out.printf("Adding %s %n", song);
//    songBook.addSong(song);
//    System.out.printf("There are %d song. %n", songBook.getSongCount());
    KaraokeMachine machine = new KaraokeMachine(songBook);;


package com.teamtreehouse;

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

import java.util.HashMap;
import java.util.Map;

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

  public KaraokeMachine(SongBook songBook) throws IOException {
    mSongBook = songBook;
    mReader = new BufferedReader(new InputStreamReader(;
    mMenu = new HashMap<String, String>();
    mMenu.put("add", "Add a new sont to the song book");
    mMenu.put("quit", "Give up? 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":
          //TODO: Add a new song
        Song song = promptNewSong();
        System.out.printf("%s added! %n%n", song);
        case "quit":
          System.out.println("Thanks for playing!");
          System.out.printf("Unknown choice: '%s'. Try again. %n%n%n", choice);
      catch(IOException ioe) {
        System.out.println("There was a problem with your input");

  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);


2 Answers

Emmanuel C
Emmanuel C
8,760 Points

In the instructors code the KaraokeMachine constructor doesnt throw an IOException. If you want to keep that you have to try catch where you create the object in the main method.

Phoenix S.
Phoenix S.
2,456 Points

For whatever reason I initially had it that way and it STILL didn't work but it worked when I removed it just now. Sooo frustrating!! Thanks for your answer! :)