[英]Getting exception while reading from a file using Scanner
I'm trying to get a program to read the contents of a text file, store each line in an array, and then output the results in an ordered fashion for each type. 我正在尝试获取一个程序来读取文本文件的内容,将每一行存储在一个数组中,然后以每种类型的有序方式输出结果。 I have the sorted part down, but every time I run the main program, I keep getting an error message for try/catch (This is still a work in progress)
我把排序的部分记下来了,但是每次运行主程序时,我都会不断收到一条错误消息,尝试/捕获(这仍在进行中)
package p20;
import java.io.*;
import java.util.*;
public class EmployeeOrderingDemo {
public static void main(String[] args) {
Scanner input=null;
ArrayList<EmployeeFX> employeeList=new ArrayList<EmployeeFX>();
try {
FileReader Info=new FileReader("P01_DATA.txt");
input=new Scanner(Info).useDelimiter("\\s\\s+");
}
catch(FileNotFoundException noFile) {
System.out.println("Can't open file");
System.exit(1);
}
try {
while(input.hasNextLine()) {
employeeList.add(new EmployeeFX(input.nextInt(),input.next(),input.next(), input.nextBoolean(), input.nextInt()));
input.nextLine();
}
}
catch(NoSuchElementException element) {
System.err.println("Wrong type of file");
System.exit(1);
}
catch(IllegalStateException state) {
System.err.println("Couldn't read from file");
System.exit(1);
}
if(input!=null) {
input.close();
}
}
}
I get the message at "Wrong type of file". 我收到“错误的文件类型”消息。 Is it because I need to skip the headers of the text file?
是因为我需要跳过文本文件的标题吗?
Here's the EmployeeFX code 这是EmployeeFX代码
package p20;
public class EmployeeFX {
private int id;
private String firstName;
private String lastName;
private boolean salaried;
private double salary;
public EmployeeFX(int id, String firstName, String lastName,boolean salaried, int salary) {
this.id=id;
this.firstName=firstName;
this.lastName=lastName;
this.salaried=salaried;
this.salary=salary;
}
}
And here's the stack trace 这是堆栈跟踪
java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at p20.EmployeeOrderingDemo.main(EmployeeOrderingDemo.java:26)
Here's the input text file 这是输入文本文件
id firstName lastName salaried salary
200 Caroline James false 37654
2 Julian James false 46499
1 Conor Habgren true 88767
10 Tillie Donalan true 98456
15 Alice Jeanu true 72821
12 Fred Habgren false 28767
103 Mary Donalan false 28456
135 Ed Jeanu true 52821
Try the below code for the main
method of the class: Find the comments inline. 对于类的
main
方法,请尝试以下代码:内联查找注释。
public static void main(String[] args) {
Scanner input=null;
ArrayList<EmployeeFX> employeeList=new ArrayList<EmployeeFX>();
try {
FileReader Info=new FileReader("P01_DATA.txt");
input=new Scanner(Info).useDelimiter("\\s+"); //Single white space regex is enough.
}
catch(FileNotFoundException noFile) {
System.out.println("Can't open file");
System.exit(1);
}
input.nextLine(); // Ignore the first line
input.nextLine(); // Ignore the second line
try {
while(input.hasNext()) { //hasNext() will check for the next available token
employeeList.add(new EmployeeFX(input.nextInt(),input.next(),input.next(), input.nextBoolean(), input.nextInt()));
} // Additional newLine() reading is not required here.
}
catch(NoSuchElementException element) {
System.err.println("Wrong type of file");
System.exit(1);
}
catch(IllegalStateException state) {
System.err.println("Couldn't read from file");
System.exit(1);
}
if(input!=null) {
input.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.