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! Custom Serialization

Udrea Mihai
Udrea Mihai
3,465 Points

Cannot find symbol..

Karaoke.java:13: error: cannot find symbol
songBook.exportTo(songs.txt);
^
symbol: variable songs
location: class Karaoke
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) { SongBook songBook = new SongBook(); songBook.importFrom("songs.txt"); KaraokeMachine machine = new KaraokeMachine(songBook); machine.run(); System.out.println("Saving book..."); songBook.exportTo(songs.txt); } }

I think it is a problem with the workshop, Karaoke.java is in the teamtreehouse folder but appear under the model folder, I tried to drag it but doesn't work. Or is an impost problem. Please help me.

package com.teamtreehouse.model;

import java.io.*;

import java.util.ArrayList; import java.util.Comparator; import java.util.TreeMap; 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>> byArtist = new TreeMap<>(); 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){ List<Song> songs = byArtist().get(artistName); songs.sort(new Comparator<Song>(){

@Override
 public int compare(Song song1, Song song2){
    if(song1.equals(song2)){
      return 0;
    }
    return song1.mTitle.compareTo(song2.mTitle);
 }
});
return songs;

}

}

1 Answer

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Udrea,

seems like you are just missing the double quotes around "songs.txt".

songBook.exportTo(songs.txt) -> songBook.exportTo("songs.txt")

Kind Regards, Florian