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

annient
PLUS
annient
Courses Plus Student 1,708 Points

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExcept

I tried to see if method importTo() in SongBook file work if I have songs.txt. This is what I got:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExcept
ion: 1
at com.teamtreehouse.model.SongBook.importFrom(SongBook.
java:43)
at Karaoke.main(Karaoke.java:17)

Please help :(

annient
annient
Courses Plus Student 1,708 Points

Here is my Karaoke.java file:

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

And my SongBook.java file:

package com.teamtreehouse.model;

import java.io.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;

public class SongBook {
  private List<Song> mSongs; 

  public SongBook() { //constructor
   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("Problem loading %s %n", fileName);
    }
  }

  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 HashMap<>();
    for (Song song: mSongs) {
      List<Song> artistSongs = byArtist.get(song.getArtist()); 
      if (artistSongs == null) { //if the song has not been defined
        artistSongs = new ArrayList<>();
        byArtist.put(song.getArtist(), artistSongs);
      }
      artistSongs.add(song); 
    }
    return byArtist;
  }

  public Set<String> getArtists() {
    return byArtist().keySet();  
  }

  public List<Song> getSongsForArtist(String artistName) {
    return byArtist().get(artistName);  
  }
}

Take a snapshot of your workspace file by clicking on the camera in the upper right. Then you can share the link when you view the snapshot. I suspect it is your songs.txt file.

2 Answers

If you "fork snapshot" you will be able to use it without having to copy my code. Your snapshot is incomplete and has a lot of errors.

The problem was your songs.txt file. It had some blank lines that were causing the error.

https://w.trhou.se/bfmncf3gqm

annient
annient
Courses Plus Student 1,708 Points

Thanks. I fixed song.txt file and it works. :)

Rashadat Mirzayev
Rashadat Mirzayev
14,601 Points

Hi, annient. How did you fix the problem, cause I have the same? Can you share pls