猿问

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

我有一个包含多行的文本文件,每行 3 个逗号分隔的条目。


例子:


苏联,Larisa_LATYNINA,18

美国,Michael_PHELPS,16

苏联,Nikolay_ANDRIAOV,15


我试图将它们分成 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());

        }

但每次我尝试运行它时都会出错。我不确定如何将文件中的每个“列”分成它们自己的数组。


如果我使用useDelimiter(",");而不是useDelimiter(" , ");我得到以下异常:


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)

这是我得到的错误。第 14 行是


Medals.add(input.nextInt());


慕侠2389804
浏览 141回答 3
3回答

喵喔喔

从您的文本示例中,分隔符不是用空格 (&nbsp;" , ") 包围的逗号,而只是一个逗号 (&nbsp;",")。删除这些空格,你应该没问题:input.useDelimiter(",");

牧羊人nacy

你得到InputMismatchException是因为最后一行input.nextInt()返回"18&nbsp;United States"由于and之间没有,分隔符(但有行分隔符),每个标记从分隔符返回到分隔符。18United States\n您应该从文件中读取所有行并按分隔符拆分它们:List<String> lines = Files.readAllLines(Paths.get("Olympic.txt"));for (String line : lines) {&nbsp; &nbsp; String[] fields = line.split(",");&nbsp; &nbsp; country.add(fields[0]);&nbsp; &nbsp; name.add(fields[1]);&nbsp; &nbsp; medals.add(Integer.valueOf(fields[2]));}

慕森卡

请将您的代码修改为:public class Main {&nbsp; &nbsp; public static void main(String[] args) throws FileNotFoundException {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> country = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> name = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> medals = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader;&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader = new BufferedReader(new FileReader("F://test.txt"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (line != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] lineParts = line.split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; country.add(lineParts[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name.add(lineParts[1]);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; medals.add(Integer.valueOf(lineParts[2]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(country);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(name);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(medals);&nbsp; &nbsp; }}在 test.xml 中:Soviet Union,Larisa_LATYNINA,18United States,Michael_PHELPS,16Soviet Union,Nikolay_ANDRIANOV,15代码输出:[Soviet Union, United States, Soviet Union][Larisa_LATYNINA, Michael_PHELPS, Nikolay_ANDRIANOV][18, 16, 15]
随时随地看视频慕课网APP

相关分类

Java
我要回答