简体   繁体   English

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

[英]java: Read text file and store the info in an array using scanner class

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. 我正在尝试从文本文件中读取此数据,并使用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)

Try useDelimiter(" \\\\$ |[\\\\r\\\\n]+"); 试试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 您需要提供行分隔符和字段分隔符

Your while loop is off. 你的while循环关闭了。

nextLine() will get you all what's left of the line and advance the cursor to there. nextLine()将获取该行的所有内容并将光标前进到那里。 nextInt() will then jump delimiters until it finds an int. 然后nextInt()将跳转分隔符,直到找到一个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. 要么您需要使用行尾作为分隔符,要么使用StringTokenizer来打破每一行,然后将每个部分解析为单独的标记。

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

相关问题 Java - 如何使用扫描仪将文本文件的值存储到数组中? - Java - How to store values of a text file into an array using Scanner? 如何使用Java中的扫描程序读取文本文件? - how to read a text file using scanner in Java? Java - 使用扫描仪读取文本文件并存储它,给我空的 ArrayList。 使用分隔符拆分单词 - Java - Read text file and store it using scanner, gives me empty ArrayList. Split words by using Delimeter 如何使用扫描仪读取文件并将值存储在二维数组中 - how to Read a file and store the values in a 2-Dimensional Array using Scanner Java:如何使用扫描仪类读取资源文件夹中的文本文件 - Java: How to use Scanner class to read text file in resources folder Java使用Scanner类从文本文件中读取每行数字,找到最大值 - Java read each lines of numbers from text file using Scanner class, finding max value 如何从扫描仪读取文本文件并将其转换为整数数组 Java - How to Read Text File From a Scanner and Convert Into an Int Array Java 使用扫描仪读取文本文件,为单词创建一个字符串,为数字生成一个int数组 - Using scanner to read text file and make a string for the words and an int array for the numbers Java 使用扫描仪从 Java 中的文本文件中获取和存储变量 - Get and store variables from a text file in Java using scanner 使用扫描仪读取文本文件 - Using Scanner to read in a text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM