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
Matthew Earlywine
2,560 PointsMy Karaoke workshop
In my workshop Karaoke I have been following the instructor putting my code in but I keep getting errors. I've looked it over and I am not finding why error or how to fix it.
package com.teamtreehouse.model;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SongBook {
private List<Song> mSongs;
public SongBook() {
mSongs = new ArrayList<Song>();
}
public void exportTo(String fileName) {
try (
FileOutputStream fos = new FileOutputStream(fileName);
PrintWriter writer = new PrintWriter(fos);
) {
for (Song song : mSongs) {
writer.printf("%s|%s|%s%n",
song.getArtist(),
song.getTitle(),
song.getVideoUrl());
}
} catch(IOException ioe) {
System.out.printf("Problem saving %s %n", fileName);
ioe.printStackTrace();
}
}
public void importFrom(String fileName) {
try (
FileInputStream fis = new FileInputStream(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
) {
String line;
while((line = reader.readLine()) != null) {
String[] args = line.split("\\|");
addSong(new Song(args[0], args[1], args[2]));
}
} catch(IOException ioe) {
System.out.printf("Problems loading %s %n", fileName);
ioe.printStackTrace();
}
}
public void addSong(Song song) {
mSongs.add(song);
}
public int getSongCount() {
return mSongs.size();
}
// FIXME: This should be cached!
private Map<String, List<Song>> byArtist() {
Map<String, List<Song>> byArtsit = new HashMap<>();
for (Song song : mSongs) {
List<Song> artistSongs = byArtist.get(song.getArtist());
if (artistSongs == null) {
artistSongs = new ArrayList<>();
byArtist.put(song.getArtist(), artistSongs);
}
artistSongs.add(song);
}
return byArtist;
}
public Set<String> getArtist() {
return byArtist().keySet();
}
public List<Song> getSongsForArtist(String artistName) {
return byArtist().get(artistName);
}
}
import com.teamtreehouse.KaraokeMachine;
import com.teamtreehouse.model.Song;
import com.teamtreehouse.model.SongBook;
public class Karaoke {
public static void main(String[] args) {
SongBook songBook = new SongBook();
songBook.importFrom("songs.txt");
KaraokeMachine machine = new KaraokeMachine(songBook);
machine.run();
System.out.println("Saving book...");
songBook.exportTo("songs.txt");
}
}
package com.teamtreehouse.model;
public class Song {
private String mArtist;
private String mTitle;
private String mVideoUrl;
public Song(String artist, String title, String videoUrl) {
mArtist = artist;
mTitle = title;
mVideoUrl = videoUrl;
}
public String getTitle() {
return mTitle;
}
public String getArtist() {
return mArtist;
}
public String getVideoUrl() {
return mVideoUrl;
}
@Override
public String toString() {
return String.format("Song: %s by %s", mTitle, mArtist);
}
}
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class KaraokeMachine {
private SongBook mSongBook;
private BufferedReader mReader;
private Map<String, String> mMenu;
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 songbook");
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. 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);
break;
case "choose":
String artist = promptArtist();
Song artistSong = promptSongForArtist(artist);
// TODO: ADd to a song queue
System.out.printf("You chose: %s %n", artistSong);
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 artist:");
List<String> artists = new ArrayList<>(mSongBook.getArtist());
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());
}
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", counter, option);
counter++;
}
String optionAsString = mReader.readLine();
int choice = Integer.parseInt(optionAsString.trim());
System.out.print("Your choice: ");
return choice -1;
}
}
Matthew Earlywine
2,560 PointsThere are three errors all in the first code which is SongBook.java
./com/teamtreehouse/model/SongBook.java:64: error: cannot find symbol
List<Song> artistSongs = byArtist.get(song.getArtist());
^
symbol: variable byArtist
location: class SongBook
./com/teamtreehouse/model/SongBook.java:67: error: cannot find symbol
byArtist.put(song.getArtist(), artistSongs);
^
symbol: variable byArtist
location: class SongBook
./com/teamtreehouse/model/SongBook.java:71: error: cannot find symbol
return byArtist;
^
symbol: variable byArtist
location: class SongBook
2 Answers
Daniel Hartin
18,106 PointsHi Matthew
Okay in the method below you have mis-spelt byArtist as byArtsit on line 2. Also, although you can get away with naming variables in Java the same name as methods I would recommend against doing this as it gets very confusing.
The rest of the code in this method looks okay, let me know how you get on with these small changes(corrected below)
private Map<String, List<Song>> byArtist() {
Map<String, List<Song>> artistMap = new HashMap<>(); //mis-spelt variable here
for (Song song : mSongs) {
List<Song> artistSongs = artistMap.get(song.getArtist());
if (artistSongs == null) {
artistSongs = new ArrayList<>();
artistMap.put(song.getArtist(), artistSongs);
}
artistSongs.add(song);
}
return artistMap;
}
Hope this helps
Daniel
Matthew Earlywine
2,560 PointsNow when I try to run it. I get Error: Could not find or load main class karaoke.
Daniel Hartin
18,106 PointsIt isn't simply because you have no package defined for the Karaoke class is it? I suspect you need to add the line
package com.teamtreehouse;
to the very top of your Karaoke class.
Daniel Hartin
18,106 PointsDaniel Hartin
18,106 PointsHi Matthew
Thanks for posting the code, do you have any idea what the error is at all? can you post the error message output or give us an idea of where the code is breaking?
Thanks Daniel