gpt4 book ai didi

java - 尝试将 Double 解析为 String 时出现 ArrayOutOfBounds 异常

转载 作者:行者123 更新时间:2023-12-02 06:35:09 25 4
gpt4 key购买 nike

我正在努力完成一项任务,其中必须将文本文件中的数据存储到 ArrayList 中,其中包括歌曲描述的字符串值以及持续时间和评级值的 double 值。

很抱歉造成困惑,我是这个网站的新手。

这是我尝试 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;
}



}

最佳答案

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

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

该行缺少第七个元素(评级)。因此,显然,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有关其他选项的更多信息,请点击此处。

关于java - 尝试将 Double 解析为 String 时出现 ArrayOutOfBounds 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19739858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com