2015-11-01 45 views
0

我试图获取一个程序来读取文本文件的内容,将每行存储在数组中,然后以每种类型的有序方式输出结果。我有排序的部分了,但我每次运行主程序时,我不断收到错误消息的try/catch(这仍然是一个进展中的工作)使用扫描器从文件中读取时得到异常

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(); 
    } 
    } 
} 

我得到“错误消息类型文件“。是否因为我需要跳过文本文件的标题?

这里的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; 
    } 
} 

而这里的堆栈跟踪

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) 

这里的输入文本文件

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 
+1

打印堆栈跟踪,并将其包括:'element.printStracktrace()' – wvdz

+0

尝试EmployeeFX类的代码添加到您的文章 – HDJEMAI

+0

java.util.InputMismatchException \t在java.util.Scanner.throwFor(未知源) \t在java.util.Scanner.next(来源不明) \t在java.util.Scanner.nextInt(来源不明) \t在java.util.Scanner.nextInt(来源不明) \t在p20.EmployeeOrderingDemo .main(EmployeeOrderingDemo.java:26) – Matt

回答

1

尝试下面的代码类的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(); 
    } 
} 
+0

这是一个类。我认为我们不允许更改文本文件 – Matt

+0

@Matt现在检查。 – YoungHobbit

+0

忽略发布此贴 – Matt