Java Java Data Structures 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
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 45,924 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