我正在努力解决必须将文本文件中的数据存储到 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;
}
}