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! Menu UI

Keep getting an error about the KaraokeMachine package in the Karaoke class.

I did add the package correctly (I guess) in the Karaoke class but it still showing me lots of error in the Karaoke and KaraokeMachine class. Can anyone tell me where I am going wrong.

Karaoke.java:

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(); KaraokeMachine machine = new KaraokeMachine(songBook); machine.run(); } }

KaraokeMachine.java :

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.HashMap; import.java.util.Map;

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

public KoraokeMachine(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 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(); mSongBook.addSong(song); System.out.printf("%s added! %n%n", song); break; case "quit" : System.out.println("Thanks for playing"); break; default: System.out.printf("Unknoun choice: '%s', Try again. %n%n%n", choice); } } catch(IOException ioe) { System.out.println("Promblem 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);
} }

2 Answers

Anders Björkland
Anders Björkland
7,481 Points

Hi Ritu!

What's the error? Are you missing the Song or SongBook class?

I'm just quickly looking over your code, and it seems like you are adding the variable songBook to the KaraokeMachine constructor without having declared or initialized such a variable. It might be that?

Yes, I was initializing the song but not the songBook, thank you for your help.