Jump to content

java code compile but didn't work

Recommended Posts

Hello everyone,


today I test code from Head First Java book about Jukebox3


(if you have this book please see on page : 537 this is code about Jukebox3, and on page 550 about class Song implements Comparable<Song> chapter 16 about collection and data Structure)


this the code about Jukebox3 :

 import  java.util.*; import  java.io.*;    public class Jukebox3 {         ArrayList<Song> songList = new ArrayList<Song>();	  	  public static void main(String[] args) {	    new Jukebox3().go();		}			  void go() {	     getSongs();		 System.out.println(songList);		 Collections.sort(songList);		 System.out.println(songList);		 }		 	  void getSongs() {	     try {		   File file = new File("src/SongList.txt");		   BufferedReader reader = new BufferedReader(new FileReader(file));		   String line = null;		   while((line = reader.readLine())!=null) {		      addSong(line);		   } // close while		 		 } catch(Exception ex) {		 ex.printStackTrace();		 }		} //close getSongs			   void addSong(String lineToParse) {	     String[] tokens = lineToParse.split("/");		 Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);		 songList.add(nextSong);		} // close addSong	} // closing class					

and this code about class Song implements Comparable<Song> :

  class Song implements Comparable<Song> {    String title;	String artist;	String rating;	String bpm;		public int compareTo(Song s) {		return title.compareTo(s.getTitle());	}		Song(String t, String a, String r, String  {	  title = t;	  artist = a;	  rating = r;	  bpm = b;	}		public String getTitle() {	  return title;	 }	 	 public String getArtist() {	  return artist;	 }	 	 public String getRating() {	  return rating;	 }	 	 public String getBpm() {	  return bpm;	 }	 	 public String toString() {	  return title;	 }	} // closing class

both of this code able to compile,


but when I run it's shown message :


java.lang.ArrayIndexOutOfBoundsException: 2
at Jukebox3.addSong(Jukebox3.java:35)
at Jukebox3.getSongs(Jukebox3.java:25)
at Jukebox3.go(Jukebox3.java:13)
at Jukebox3.main(Jukebox3.java:9)
Q : Why this code can be compile but when I run this code, it shown like on above?
whereas, on Head First Java this code can be run well
please help me
Link to post
Share on other sites

It says ArrayIndexOutOfBounds, which means that one of the indices is too high or negative.

It's probably happening on this line:

Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);

Since the array is being created by splitting a string then you have to check that the string is formed correctly. The strings are coming from src/SongList.txt so check the contents of that file. Show it here.

Link to post
Share on other sites

I had delete src/SongList.txt, and change it to : File file = new File("SongList.txt"); but the result :


java.io.FileNotFoundException: SongList.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at Jukebox3.getSongs(Jukebox3.java:22)
at Jukebox3.go(Jukebox3.java:13)
at Jukebox3.main(Jukebox3.java:9)
and about line :
Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);

when I delete or add the one of array for instance : I delete tokens[3] or I add token[4] it won't compile.


Q : how to write directory path on File file = new File("SongList.txt"); so, that java .io able to find SongList.txt without using "src/SongList.txt"?


please answer



Link to post
Share on other sites

If the file is in the src folder, then you need to put that in the path. You need to give it the correct path, you can't point it to a file that doesn't exist. Either move the file or give it the correct path, that should be obvious.




Why this code can be compile but when I run this code, it shown like on above?

Array indexes like that cannot be checked at compile time. You are seeing a run-time error, not a compile-time error. It is your responsibility as a programmer to make sure that the array indexes exist before you try to use them.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...