java:使用扫描仪类读取文本文件并将信息存储在数组中

发布于 2024-08-25 05:14:53 字数 1238 浏览 2 评论 0原文

我有一个包含学生成绩的文本文件,例如:

Kim $ 40 $ 45
Jack $ 35 $ 40

我正在尝试从文本文件中读取此数据并使用扫描仪类将信息存储到数组列表中。任何人都可以指导我正确编写代码吗?

代码

import java.io.*;
import java.util.*;

public class ReadStudentsGrade {

public static void main(String[] args) throws IOException {

    ArrayList stuRec = new ArrayList();
    File file = new File("c:\\StudentGrade.txt");
    try {
        Scanner scanner = new Scanner(file).useDelimiter("$");

        while (scanner.hasNextLine())
        {
            String stuName = scanner.nextLine();
            int midTirmGrade = scanner.nextInt();
            int finalGrade = scanner.nextInt();
            System.out.println(stuName + " " + midTirmGrade + " " + finalGrade);
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
}

运行时错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at writereadstudentsgrade.ReadStudentsGrade.main(ReadStudentsGrade.java:26)

I have a text file include Student Grades like:

Kim $ 40 $ 45
Jack $ 35 $ 40

I'm trying to read this data from the text file and store the information into an array list using Scanner Class. Could any one guide me to write the code correctly?

Code

import java.io.*;
import java.util.*;

public class ReadStudentsGrade {

public static void main(String[] args) throws IOException {

    ArrayList stuRec = new ArrayList();
    File file = new File("c:\\StudentGrade.txt");
    try {
        Scanner scanner = new Scanner(file).useDelimiter("$");

        while (scanner.hasNextLine())
        {
            String stuName = scanner.nextLine();
            int midTirmGrade = scanner.nextInt();
            int finalGrade = scanner.nextInt();
            System.out.println(stuName + " " + midTirmGrade + " " + finalGrade);
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
}

Runtime error:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at writereadstudentsgrade.ReadStudentsGrade.main(ReadStudentsGrade.java:26)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖树树初阳… 2024-09-01 05:14:53

尝试 useDelimiter(" \\$ |[\\r\\n]+");

        String stuName = scanner.next(); // not nextLine()!
        int midTirmGrade = scanner.nextInt();
        int finalGrade = scanner.nextInt();

您的问题是:

  • 您错误地读取了整行来获取学生姓名
  • $ 是需要转义的正则表达式元字符
  • 您需要同时提供行分隔符和字段分隔符

Try useDelimiter(" \\$ |[\\r\\n]+");

        String stuName = scanner.next(); // not nextLine()!
        int midTirmGrade = scanner.nextInt();
        int finalGrade = scanner.nextInt();

Your problems were that:

  • You mistakenly read whole line to get student name
  • $ is a regex metacharacter that needs to be escaped
  • You need to provide both line delimiters and field delimiters
梦忆晨望 2024-09-01 05:14:53

您的 while 循环已关闭。

nextLine() 将获取该行剩下的所有内容并将光标前进到那里。
nextInt() 然后将跳转分隔符,直到找到 int。
结果将是跳过值。
假设 Kim 和 Jack 在不同的线路上,您会得到:

stuName == "Kim $ 40 $ 45"
midTirmGrade == 35
finalGrade == 40

作为您的输出;这不是你想要的。

您需要使用行尾作为分隔符或使用 StringTokenizer 来分解每一行,然后将每个部分解析为单独的标记。

Your while loop is off.

nextLine() will get you all what's left of the line and advance the cursor to there.
nextInt() will then jump delimiters until it finds an int.
The result will be skipping of values.
Assuming Kim and Jack were on different lines you would get:

stuName == "Kim $ 40 $ 45"
midTirmGrade == 35
finalGrade == 40

as your output; which isn't what you want.

Either you need to use the end-of-line as the delimiter or use a StringTokenizer to break each line up and then parse each of the sections as individual tokens.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文