Первый класс должен получить информацию из txt-файла из второго класса и поместить информацию в отдельные списки для запуска второго класса.Ожидаемый результат - это звание альбома, год, исполнитель и альбом в отдельных списках.Возвращает [1, 2, 1966] для списка MyRanks, когда должен возвращать [1, 2, 3, 4 и т. Д.]
import java.util.ArrayList;
import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class AlbumList
{
List<String> MyAlbums = new ArrayList<>();
List<String> MyArtists = new ArrayList<>();
List<Integer> MyYears = new ArrayList<>();
List<Integer> MyRanks = new ArrayList<>();
public AlbumList()
{
String album = "";
String artist = "";
int year = 0;
int albumRank = 0;
}
/**
method to read through the albums within a file
@param filename the name of a file
@return its ability to find the file and read through it
*/
public boolean readAlbums(String filename)
{
try
{
Scanner in = new Scanner(new File(filename));
in.useDelimiter(",");
in.nextLine();
String firstLine = in.nextLine();
Scanner in2 = new Scanner(firstLine);
in2.useDelimiter(",");
int rank1 = in2.nextInt();
int year1 = in2.nextInt();
String album1 = in2.next();
String artist1 = in2.next();
MyRanks.add(rank1);
MyYears.add(year1);
MyAlbums.add(album1);
MyArtists.add(artist1);
in.useDelimiter(",");
while (in.hasNextLine())
{
MyRanks.add(in.nextInt());
System.out.println(MyRanks);
MyYears.add(in.nextInt());
MyAlbums.add(in.next());
MyArtists.add(in.next());
}
System.out.println(MyRanks);
System.out.println(MyYears);
System.out.println(MyAlbums);
System.out.println(MyArtists);
}
catch (FileNotFoundException e)
{
System.out.println(e.toString());
return false;
}
return true;
}
/**
* This program can be used to test the AlbumList class methods.
*
* @author Dr.C.
*/
public class P2Test
{
public static void main(String[] args)
{
AlbumList albums = new AlbumList();
System.out.println("File opened successfully? " +
albums.readAlbums("albumlist10.csv"));
}