テキスト ファイルからのデータを 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;
}
}