2017-10-29 39 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();