0

我正在努力解决必须将文本文件中的数据存储到 ArrayList 中的任务,这包括歌曲描述的字符串值,以及持续时间和评分值的双精度值。

对不起,我是这个网站的新手。

这是我尝试 printSongs() 时显示的错误

**Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at Song.printSongs(Song.java:181)
    at Song.Submenu(Song.java:106)
    at Song.mainChoice(Song.java:85)
    at Song.main(Song.java:208)**

这是我的代码:

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.nio.charset.Charset;
import java.lang.Iterable;


public class Song 
{


public int songID;
    public String title;
    public String artist;
    public double duration;
    public String genre;
    public double rating;
    public String album;
    public static Scanner keyboard;
    public static Scanner fileIn;
    public static int choice = 0;

  @Override
   public String toString() 

  {
                return ("ID: "+this.getSongID() + " " +
                        "Title: "+ this.getTitle() + " " +
                        "Artist: "+ this.getArtist() + " " +
                        "Album: "+ this.getAlbum() + " " +
                        "Duration: "+ this.getDuration() + " " +
                        "Genre: "+ this.getGenre() + " " +
                        "Rating: "+ this.getRating() + " ");




   }



public Song(int songID, String title, String artist, String album,
        double duration, String genre, double rating) 
{
    this.songID = songID;
    this.title = title;
    this.artist = artist;
    this.genre = genre;
    this.album = album;
    this.duration = duration;
    this.rating = rating;









}




public static void displayAll() throws FileNotFoundException
{



}

public static void mainChoice()
{
    System.out.println("Make your selection:");
    System.out.println("-------------------");
    System.out.println("1. Songs");
    System.out.println("2. Playlists");
    System.out.println("3. Import/Rip CD");
    System.out.println("4. Save");
    System.out.println("5. Exit Program");
    System.out.println("-------------------");
    keyboard = new Scanner(System.in);
    choice = keyboard.nextInt();
    Submenu();      
}

public static void Submenu()
{
    System.out.println("-------------------");

    if (choice == 1)
    {
        System.out.println("     SONGS     ");
        System.out.println("1. Display Songs");
        System.out.println("2. Sort Songs");
        System.out.println("3. Rate Song");
        System.out.println("4. Set Genre");
        System.out.println("5. Exit Submenu");
        System.out.println("-------------------");
        keyboard = new Scanner(System.in);
        choice = keyboard.nextInt();

        if (choice == 1)
        {
            printSongs();
        }

        if (choice == 2)
        {

        }

        if (choice == 3)
        {

        }

        if (choice == 4)
        {

        }

        if (choice == 5)
        {
            System.out.println("-------------------");
            mainChoice();
        }

    }

    if (choice == 2)
    {
        System.out.println("   PLAYLISTS     ");
        System.out.println("1. Display");
        System.out.println("2. Create Playlist");
        System.out.println("3. Add Song");
        System.out.println("4. Exit Submenu");
        System.out.println("-------------------");
        keyboard = new Scanner(System.in);
        choice = keyboard.nextInt();

        if (choice == 1)
        {

        }

        if (choice == 2)
        {

        }

        if (choice == 3)
        {

        }

        if (choice == 4)
        {
            System.out.println("-------------------");
            mainChoice();
        }
    }


    }


public static void printSongs()
{
       ArrayList<Song> songs = new ArrayList<>(); 



        try (BufferedReader input = new BufferedReader(new InputStreamReader(
                new FileInputStream("songCollection.txt"), Charset.forName("UTF-8")))) {
            String line;
            while ((line = input.readLine()) != null) 
            {
                String[] arr = line.split(",");
                songs.add(new Song(Integer.parseInt(arr[0]), arr[1], arr[2], arr[3], Double.parseDouble(arr[4]), arr[5], Double.parseDouble(arr[6]) ));
            }


        } catch (IOException err) 
        {
            err.printStackTrace();
        }



        for (Iterator<Song> iterator = songs.iterator(); iterator
                .hasNext();) 
        {
            System.out.println(songs); 
        }


}






public static void main(String[] args) throws FileNotFoundException 
{
   mainChoice();



}






public String getAlbum() {
    return album;
}






public void setAlbum(String album) {
    this.album = album;
}






public double getRating() {
    return rating;
}






public void setDuration(int duration) {
    this.duration = duration;
}




public void setRating(int rating) {
    this.rating = rating;
}






public String getGenre() {
    return genre;
}






public void setGenre(String genre) {
    this.genre = genre;
}






public double getDuration() {
    return duration;
}






public String getArtist() {
    return artist;
}






public void setArtist(String artist) {
    this.artist = artist;
}






public String getTitle() {
    return title;
}






public void setTitle(String title) {
    this.title = title;
}






public int getSongID() {
    return songID;
}






public void setSongID(int songID) {
    this.songID = songID;
}



}
4

2 回答 2

2

您说输入文件中的违规行是:

12,Pineapple Head,Crowded House,Recurring Dream,3.5,Rock

该行缺少第 7 个元素(评级)。因此,很明显,String.split()将返回一个包含 6 个项目而不是 7 个项目的数组,并且尝试访问第 7 个项目会给您一个ArrayIndexOutOfBoundsException因为,好吧,数组索引超出范围。

当您读取输入文件时,您必须确保您的程序根据您定义的有关如何格式化输入文件的规则读取数据。您声明您的输入文件可能缺少评级数据,但您的程序假定每行包含 7 个元素,因此您的程序没有遵循您的规则,当它中断时您不会感到惊讶!

简单的解决方案是检查line.split(",")返回的数组的长度并相应地处理数据。如果有 7 个元素,那么您拥有每个字段。如果有 6 个元素,则缺少评级。如果除了 6 或 7 之外还有其他内容,则输入行格式错误——由您决定如何处理(崩溃、忽略或打印错误,无论您决定如何)。

此外,默认情况下,String.split()不返回尾随空字段,这听起来像是您想要的,但请查看http://docs.oracle.com/javase/7/docs/api/java/lang/String。 html#split%28java.lang.String,%20int%29在此处了解有关其他选项的更多信息。

于 2013-11-02T07:57:43.613 回答
0

当您拆分文本行并使用不同的拆分初始化新行时Exception,由于您拥有的代码而引发了运行时。printSongsSong

您要么假设您的文本格式错误,要么您的文本本身在某处格式错误。

简而言之,您正在引用动态创建的拆分String数组的元素,但该元素不存在。

String[] arr = line.split(",");
songs.add(new Song(Integer.parseInt(arr[0]), arr[1], arr[2], arr[3], 
                                    // that's your problem
Double.parseDouble(arr[4]), arr[5], Double.parseDouble(arr[6]) ));
于 2013-11-02T07:39:00.640 回答