如何将字符串拆分为变量和数组

时间:2017-10-29 01:30:43

标签: java

我需要读取一个包含字符串的文本文件 例如 aerosmith 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

我需要把它分成艺术家(aerosmith)专辑(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]

每一行都应该有自己的歌曲列表,不重复和添加 任何建议将不胜感激。

4 个答案:

答案 0 :(得分:0)

更好的做法是声明一个按行迭代的新扫描程序。

我将如何做到这一点:

Handler

答案 1 :(得分: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;
    }
}

答案 2 :(得分:0)

我不知道Album中的albumTracks是否是List,所以我假设它是根据你的问题而在下面是你可以尝试的。您需要导入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();

答案 3 :(得分:-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();