2017-10-29 89 views
0

我需要閱讀,有一個字符串 例如 史密斯飛船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] 

每行應該有自己的歌曲列表不重複,並添加 任何建議,將不勝感激。

回答

0

這將是一個更好的做法,聲明一個新的掃描儀在該行迭代。

這裏是我會怎麼做:

List<Album> albums = new ArrayList<Album>(); 
String artistName = "", albumName = ""; 
ArrayList<String> albumTracks; 

while (inFile.hasNextLine()) { 
    Scanner inLine = new Scanner(inFile.nextLine()); 
    if(inLine.hasNext()) artistName = inLine.next(); 
    if(inLine.hasNext()) albumName = inLine.next(); 
    albumTracks = new ArrayList<>(); 

    while (inLine.hasNext()) { 
     albumTracks.add(inLine.next()); 
    } 

    albums.add(new Album(artistName, albumName, albumTracks)); 
} 
+0

多於一個歌曲我試過這和它只准備文本文件的第一行 –

+0

嘗試它我自己,它的工作,你是否宣佈inFile像你的位置t?發送給我你的代碼從pastebin或類似的 –

+0

這個問題與此代碼,我的是不會讓我把列表轉換爲arrayLists。albumTracks是一個arraylist在構造函數 –

-1

您在while循環之外聲明瞭arraylist。這會導致ArrayList添加掃描器讀取的所有歌曲。 的代碼應該是:

File aFile = new File("catalog2.txt"); 
    Scanner inFile = new Scanner(aFile); 

    catalog = new ArrayList<Album>(); 

    while (inFile.hasNextLine()) { 

     //Declare inside the loop 
     ArrayList<String> albumTracks = new ArrayList<String>(); 
     /**********************/ 
     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(); 
+0

我想這一個和它只會添加專輯輸出的第一個兒子看起來likertistName = 98_degrees ALBUMNAME = 98_degrees_and_rising曲目= [簡介] ARTISTNAME =披ALBUMNAME = A_Hard_Day's_Night曲目= [A_Hard_Day's_Night] ARTISTNAME =阿瓦隆ALBUMNAME = a_maze_of_grace曲目= [Testify_To_Love] ARTISTNAME =披ALBUMNAME = abbey_road軌道= [Come_Together] ARTISTNAME = U2 ALBUMNAME = achtung_baby軌道= [Zoo_Station]有在每個相冊 –

0

首先只是刪除您輸入文本的所有多餘的空格使用line.split(" ");

while (inFile.hasNextLine()) { 
     String line = inFile.nextLine(); 
     line = line.replace(" = ", "=") 
       .replace("= ", "=") 
       .replace(" =", "=") 
       .replace(", ", ",") 
       .replace(" , ", ",") 
       .replace(" ,", ","); 

     String[] details = line.split(" "); 
     String artistName = details[0].split("=")[1]; 
     String albumName = details[1].split("=")[1]; 
     String tracks = details[2].split("=")[1]; 
     Album myAlbums = 
       new Album(
         artistName, 
         albumName, 
         tracks.substring(1, tracks.length() - 1).split(",") 
       ); 
     catalog.add(myAlbums); 
    } 

專輯類別:

class Album { 

    private final String artistName; 
    private final String albumName; 
    private final String[] albumTracks; 

    public Album(String artistName, String albumName, String[] albumTracks) { 
     this.artistName = artistName; 
     this.albumName = artistName; 
     this.albumTracks = albumTracks; 
    } 
} 
0

我不知道Album中的albumTracks是否是一個List,所以我假設它是根據你的問題,下面是你的c一個嘗試。你需要導入java.util.Arrays

我在這裏還假設你的數據至少有3個單詞你需要把空和大小檢查。

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]; 
    // find location of 3rd word and create list of albums from there 
    List<String> albumTracks = Arrays.asList(line.substring(line.indexOf(details[1]) + details[1].length() + 1).split(「 「)); 
    Album myAlbums = new Album(artistName, albumName, albumTracks); 
    catalog.add(myAlbums); 
} 
inFile.close();