java - Getting exception while reading from a file using Scanner -
i'm trying program read contents of text file, store each line in array, , output results in ordered fashion each type. have sorted part down, every time run main program, keep getting error message try/catch (this still 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 file"); system.exit(1); } if(input!=null) { input.close(); } } }
i message @ "wrong type of file". because need skip headers of text file?
here's employeefx code
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 stack trace
java.util.inputmismatchexception @ java.util.scanner.throwfor(unknown source) @ java.util.scanner.next(unknown source) @ java.util.scanner.nextint(unknown source) @ java.util.scanner.nextint(unknown source) @ p20.employeeorderingdemo.main(employeeorderingdemo.java:26)
here's 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 below code main
method of class: find comments inline.
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 enough. } catch(filenotfoundexception nofile) { system.out.println("can't open file"); system.exit(1); } input.nextline(); // ignore first line input.nextline(); // ignore second line try { while(input.hasnext()) { //hasnext() check next available token employeelist.add(new employeefx(input.nextint(),input.next(),input.next(), input.nextboolean(), input.nextint())); } // additional newline() reading 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 file"); system.exit(1); } if(input!=null) { input.close(); } }
Comments
Post a Comment