简体   繁体   English

以逗号分隔的文本文件到数组

[英]Separate comma delimited text file to array

I have a text file with multiple lines, and 3 comma delimited entries per line. 我有一个包含多行的文本文件,每行3个逗号分隔的条目。

Example: 例:

Soviet Union,Larisa_LATYNINA,18 苏联,Larisa_LATYNINA,18
United States,Michael_PHELPS,16 美国,Michael_PHELPS,16
Soviet Union,Nikolay_ANDRIANOV,15 苏联,Nikolay_ANDRIANOV,15

I'm trying to separate them into 3 arrays, with 1 for the country, 1 for the person, and 1 for the medals. 我正在尝试将它们分为3组,其中1组代表国家,1组代表个人,1组代表奖牌。

        ArrayList<String> country = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Integer> medals = new ArrayList<>();
        java.io.File file = new java.io.File("Olympics.txt");
        Scanner input = new Scanner(file);
        input.useDelimiter(" , ");
        while(input.hasNext()) {
            country.add(input.next());
            name.add(input.next());
            medals.add(input.nextInt());
        }

but I get a error every time I try to run it. 但是每次尝试运行它时都会出错。 I'm not exactly sure how to separate each "column" in the file into their own array. 我不完全确定如何将文件中的每个“列”分成各自的数组。

If I use useDelimiter(","); 如果我使用useDelimiter(","); instead of useDelimiter(" , "); 而不是useDelimiter(" , "); I am getting the follow exception: 我收到以下异常:

Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at OlympicWinners.main(OlympicWinners.java:14)

This is the error I get. 这是我得到的错误。 Line 14 is 第14行是

Medals.add(input.nextInt());

From your text sample, the delimiter isn't a comma surrounded with spaces ( " , " ), but just a comma ( "," ). 在您的文本示例中,定界符不是用空格( " , " )包围的逗号,而只是逗号( "," )。 Remove those spaces and you should be OK: 删除那些空格,您应该可以:

input.useDelimiter(",");

You are getting InputMismatchException because of the last line input.nextInt() returns 由于最后一行input.nextInt()返回,因此您正在获取InputMismatchException

"18 
United States"

Since there is no , delimiter between 18 and United States (but there is line separator \\n ) each token is returned from delimiter to delimiter. 由于没有,18United States之间有定界符(但有行分隔符\\n ),每个标记都从定界符返回到定界符。

You should read all lines from file and split them by separator: 您应该从文件中读取所有行,并用分隔符将它们分开:

List<String> lines = Files.readAllLines(Paths.get("Olympic.txt"));

for (String line : lines) {
    String[] fields = line.split(",");
    country.add(fields[0]);
    name.add(fields[1]);
    medals.add(Integer.valueOf(fields[2]));
}

Please modify your code to this : 请为此修改您的代码:

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> country = new ArrayList<>();
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Integer> medals = new ArrayList<>();
        BufferedReader reader;
        try {
            reader = new BufferedReader(new FileReader("F://test.txt"));
            String line = reader.readLine();
            while (line != null) {
                String[] lineParts = line.split(",");
                country.add(lineParts[0]);
                name.add(lineParts[1]); 
                medals.add(Integer.valueOf(lineParts[2]));
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(country);
        System.out.println(name);
        System.out.println(medals);

    }

}

In test.xml : 在test.xml中:

Soviet Union,Larisa_LATYNINA,18
United States,Michael_PHELPS,16
Soviet Union,Nikolay_ANDRIANOV,15

Output of the code : 代码输出:

[Soviet Union, United States, Soviet Union]
[Larisa_LATYNINA, Michael_PHELPS, Nikolay_ANDRIANOV]
[18, 16, 15]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM