Why I am getting array bound exception?
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at SongList1.main(SongList1.java:25) Java Result: 1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
import songlist1.Song1;
public class SongList1
{
public static void main(String[] args) throws FileNotFoundException
{
final int MAX_SONGS = 75;
String aSong;
String aTitle;
String aArtist;
String aAlbum;
String[] tokens;
int count = 0;
Song1[] songs = new Song1[MAX_SONGS];
String filename = args[0];
Scanner infile = new Scanner(new File(filename));
Scanner input = new Scanner(System.in);
while(infile.hasNextLine() && count < MAX_SONGS)
{
aSong = infile.nextLine();
tokens = aSong.split(":");
aTitle = tokens[0].trim();
aArtist = tokens[1].trim();
aAlbum = tokens[2].trim();
songs[count] = new Song1(aTitle, aArtist, aAlbum);
count++;
}
infile.close();
System.out.printf("%-15s%-15s%-15s\n", "Title", "Artist", "Album");
System.out.println("-----------------------------------------------");
for(int i = 0; i < count; i++)
System.out.printf("%-15s%-15s%-15s\n", songs[i].getTitle(),
songs[i].getArtist(), songs[i].getAlbum());
if(count == 0)
System.out.println("No songs are loaded!");
Arrays.sort(songs, 0, count);
System.out.print("\nEnter the name of an artist: ");
aArtist = input.nextLine();
findResults(songs, count, aArtist);
}
public static void findResults(Song1[] songs, int count, String aArtist)
{
boolean isFound = false;
System.out.println("\nSongs by " + aArtist + ":");
System.out.printf("%-15s%-15s\n", "Album", "Title");
System.out.println("--------------------------------");
for(int i = 0; i < count; i++)
{
if(songs[i].getArtist().toLowerCase().equals(aArtist.toLowerCase()))
{
isFound = true;
System.out.printf("%-15s%-15s\n", songs[i].getAlbum(),
songs[i].getTitle());
}
}
if(!isFound)
System.out.println("\tNo song is found!");
}
}
Aucun commentaire:
Enregistrer un commentaire