我需要阅读,有一个字符串 例如 史密斯飞船toys_in_the_attic Toys_In_The_Attic Uncle_Salty Adam's_Apple Walk_This_Way Big_Ten_Inch_Record Sweet_Emotion No_More_No_More Round_And_Round You_See_Me_Crying的如何将字符串分割成变量,数组
我需要把它们分割文本文件艺术家(史密斯飞船)专辑(toy_in_the_attic)和所有的歌曲只(Toys_In_The_Attic Uncle_Salty Adam's_Apple Walk_This_Way Big_Ten_Inch_Record Sweet_Emotion No_More_No_More Round_And_Round You_See_Me_Crying)到一个数组列表
这是代码我有
File aFile = new File("catalog2.txt");
Scanner inFile = new Scanner(aFile);
catalog = new ArrayList<Album>();
while (inFile.hasNextLine())
{
String line = inFile.nextLine();
String[] details = line.split(" ");
String artistName = details[0];
String albumName = details[1];
albumTracks.add(details[2]);
Album myAlbums = new Album(artistName, albumName, albumTracks);
catalog.add(myAlbums);
}
inFile.close();
会发生什么是出看起来像这样
artistName = aerosmith albumName = toys_in_the_attic tracks = [Toys_In_The_Attic]
artistName = aerosmith albumName = permanent_vacation tracks = [Toys_In_The_Attic, Heart's_Done_Time]
artistName = aerosmith albumName = pump tracks = [Toys_In_The_Attic, Heart's_Done_Time, Young_Lust]
每行应该有自己的歌曲列表不重复,并添加 任何建议,将不胜感激。
多于一个歌曲我试过这和它只准备文本文件的第一行 –
尝试它我自己,它的工作,你是否宣布inFile像你的位置t?发送给我你的代码从pastebin或类似的 –
这个问题与此代码,我的是不会让我把列表转换为arrayLists。albumTracks是一个arraylist在构造函数 –