您的问题有点难以理解,但我试了一下并想象了这个场景。我假设您已经创建了 Album 和 Track 类并且一切正常。我假设您的相册文件如下所示:
Pink Floyd : Dark Side of the Moon
0:01:30 - Speak to me
0:06:48 - Brain Damage
Another artist: Album name
0:02:33 - Whatever
0:16:21 - Blah Blah
Third artist: Album name
0:02:33 - X
0:16:21 - Y
0:02:33 - Z
0:16:21 - A
您要做的是开始逐行读取文件,我相信您正在代码中我们看不到的某个地方执行此操作。对于每一行,您都有以下条件,这很好
//you don't need to add == true in the if condition
if (Character.isUpperCase(line.charAt(0))) {
//Album found
} else {
//Track found
}
每次阅读一行。如果找到专辑,则初始化专辑对象并将其存储艺术家、标题和一个空的曲目列表。每次找到曲目时,检查专辑对象是否不为空(如果不为空则为当前专辑)并检索其曲目列表,将新曲目添加到其中并将曲目列表设置回专辑对象。
我已经编写了以下代码,假设您拥有我们在这个问题中看不到的大部分代码。通过代码,您将了解如何读取一行以及如何创建专辑对象,如何创建曲目并将其存储在专辑中。要测试以下解决方案,请将其复制/粘贴到与类名相同的文件中并执行它,确保您拥有包含专辑的 album.txt 文件。
import java.util.List;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class Track {
String track;
public Track(String track) {
this.track = track;
}
//overriding toString() method for Track class
public String toString() {
return track;
}
}
class Album {
String artistName;
String albumTitle;
List < Track > tracks = new ArrayList < Track > ();
//Album Constructor
public Album(String artistName, String albumTitle) {
this.artistName = artistName;
this.albumTitle = albumTitle;
}
public List < Track > getTracks() {
return tracks;
}
public void setTracks(List < Track > tracks) {
this.tracks = tracks;
}
//overriding the toString method for Album
public String toString() {
StringBuilder album = new StringBuilder();
album.append("Artist name: " + artistName + "\n");
album.append("\n Album title : " + albumTitle + "\n\n");
for (int i = 0; i < tracks.size(); i++) {
album.append("\n Track " + (i + 1) + ":" + tracks.get(i).toString());
}
return album.toString();
}
}
public class ReadAlbums {
public static void main(String[] args) {
List < Album > albumsCollection = new ArrayList < Album > ();
BufferedReader in = null;
try { in = new BufferedReader(new FileReader("albums.txt"));
String line;
List < Track > currentTracks = new ArrayList < Track > ();
Album album = null;
while ((line = in .readLine()) != null) {
//no need to put == true in the if condition
if (Character.isUpperCase(line.charAt(0))) {
//Album found
String[] token = line.split(":");
//If the not the first ever album then add the previous album to the collection
if (album != null) {
albumsCollection.add(album);
}
//new album object is created with artist name and album title
album = new Album(token[0], token[1]);
//new empty track list is added to the album object
album.setTracks(new ArrayList < Track > ());
} else {
//Track found
//retrieve the track from Album album
currentTracks = album.getTracks();
//Add the track to the list of tracks obtained from album
currentTracks.add(new Track(line));
//add the updated track list back to the album object
album.setTracks(currentTracks);
}
}
//add the last album in the album collections
if(album != null) {
albumsCollection.add(album);
}
//close the input stream
in.close();
} catch (IOException e) {}
System.out.println("albums : " + albums.toString());
}
}
你会得到以下输出:
albums : [Artist name: Pink Floyd
Album title : Dark Side of the Moon
Track 1:0:01:30 - Speak to me
Track 2:0:06:48 - Brain Damage, Artist name: Another artist
Album title : This is the second album
Track 1:0:02:33 - Whatever
Track 2:0:16:21 - Blah Blah, Artist name: Third artist
Album title : This is the third album
Track 1:0:02:33 - X ]
将从文件中读取的数据以相同的格式打印回来。您需要遍历专辑并为每个专辑检索曲目列表,然后打印曲目。
for(int i = 0; i < albumsCollection.size();i++) {
Album album = albumsCollection.get(i);
System.out.println(album.getArtistName() + ":" + album.getAlbumTitle());
List<Tracks> tracks = album.getTracks();
for(int j = 0; j < tracks.size(); j++) {
System.out.println(tracks[j].toString());
}
}