java:使用扫描仪类读取文本文件并将信息存储在数组中
我有一个包含学生成绩的文本文件,例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
useDelimiter(" \\$ |[\\r\\n]+");
您的问题是:
$
是需要转义的正则表达式元字符Try
useDelimiter(" \\$ |[\\r\\n]+");
Your problems were that:
$
is a regex metacharacter that needs to be escaped您的 while 循环已关闭。
nextLine()
将获取该行剩下的所有内容并将光标前进到那里。nextInt()
然后将跳转分隔符,直到找到 int。结果将是跳过值。
假设 Kim 和 Jack 在不同的线路上,您会得到:
作为您的输出;这不是你想要的。
您需要使用行尾作为分隔符或使用 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:
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.