我正在努力完成一项任务,其中来自文本文件的数据必须存储到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;
}
}
答案 0 :(得分: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以获取有关此处其他选项的更多信息。
答案 1 :(得分:0)
由于您在Exception
中的代码,当您拆分文本行并使用不同的拆分初始化新的printSongs
时,会引发运行时Song
。
您要么为文本采用错误的格式,要么文本本身在某处出现格式错误。
简而言之,您正在引用动态创建的split 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]) ));