Results 1 to 3 of 3
- 05-01-2012, 08:28 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
File input throwing NoSuchElementException exception
I am trying to read a file and store its contents into my LinkedList.
The file contains each object of the linked list on a new line, and each element in the object is seperated by a comma and no whitespace. Ive set the delimeter up to just have "," as there are titles seperated by whitespace and i dont want it to read these as two seperate tokens.
Java Code:public Registry() { assignmentList = new LinkedList<Assignment>(); try { File inFile = new File("Assignment.txt"); Scanner in = new Scanner(inFile); in.useDelimiter(","); while(in.hasNextLine()) { String mTitle = in.next(); String mIdentifier = in.next(); String aTitle = in.next(); String dateSet = in.next(); String dueDate = in.next(); String aAuthor = in.next(); String weighting = in.next(); addAssignment(new Assignment(mTitle,mIdentifier,aTitle,dateSet,dueDate,aAuthor,weighting)); } in.close(); } catch(FileNotFoundException exception) { System.out.println("FileNotFoundException " + exception); } catch(IOException exception) { System.out.println("IOException " + exception); } }
Java 2,ASM 1001,Programming Project,13/05/2012,20/05/2012,Joe Bloggs,20%
Java 1,ASM 1002,Another Programming Project,27/05/2012,05/06/2012,Joe Bloggs,20%
and the error message is
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at Registry.<init>(Registry.java:34)
at FileTester.main(FileTester.java:5)
Java Result: 1
at Registry.<init>(Registry.java:34) refers to this line String dateSet = in.next(); if that helps
- 05-02-2012, 06:38 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: File input throwing NoSuchElementException exception
Isn't it be better to read the text line by line and then split each line into the individual data? Using the program above if you check the weighting variable value it will contains value like 20% Java 1 because you only tell the scanner to delimit the entry by comma.
And at some point this will produce the NoSuchElementException because no more data to read when you call the next() method. It's because the weighting took two values instead of one.Website: Learn Java by Examples
- 05-02-2012, 02:34 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: File input throwing NoSuchElementException exception
I have changed the format of the .txt file now to add a delimter at the start of each line which now works.
I thought as i was saying while(in.hasNextLine) it would read all the tokens from that line and then start again from the new line without the need for a delimeter at the start. I guess not, also i will have a look what you suggested about reading line by line and splitting it.
Similar Threads
-
Program not throwing exception
By aedara in forum New To JavaReplies: 3Last Post: 06-28-2011, 06:55 AM -
exception in thread main java.util.nosuchelementexception
By dude1it in forum New To JavaReplies: 6Last Post: 03-11-2011, 04:53 AM -
Exception throwing
By andy16 in forum New To JavaReplies: 9Last Post: 06-07-2010, 06:57 PM -
[SOLVED] why am i getting this exception" java.util.NoSuchElementException
By ariz in forum New To JavaReplies: 5Last Post: 02-27-2009, 06:19 AM -
throwing Exception
By bugger in forum New To JavaReplies: 3Last Post: 11-09-2007, 10:35 PM
Bookmarks