我需要閱讀,有一個字符串 例如 史密斯飛船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在構造函數 –