Results 1 to 7 of 7
Thread: NoSuchElementException... help!!
- 03-19-2011, 10:07 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
NoSuchElementException... help!!
Hello,
I'm new to java and I'm attempting to write a simple database using static arrays.
The problem that i cannot get past is in a method that reads a text file into the array.
Below is the .txt file that the method is reading from.
(note: "|" is used as the delimeter and there are not supposed to be lines between each entry in the .txt file.)
Java Code:Bob|Marley|1993@yahoo.com|Monday, Tuesday, Thursday before and after school|Intermediate|CompSci John|Doe|jd@yahoo.com|Monday, Thursday after school|Novice|CompSci Billy|Mays|bm@yahoo.com|Monday, Tuesday before school|Expert|CompSci Fred|Flintstone|fff@yahoo.com|Monday, Tuesday, Thursday before and after school|Newbie|CompSci
And this is the file reading method:
Java Code:public void read(File f) { //clear array members = new TeamMember[100]; count = 0; Scanner input = null; try { input = new Scanner(f); input = input.useDelimiter("|"); while(input.hasNext()) { String first = input.next(); String last = input.next(); String email = input.next(); String avail = input.next(); String skill = input.next(); String exp = input.next(); if(input.hasNext()) { input.nextLine(); } TeamMember temp = new TeamMember(first, last, email, avail, skill, exp); addMember(temp); count++; } input.close(); } catch (FileNotFoundException e) { System.out.println("/n ERROR: " + e.getMessage()); } }
I have not the slightest clue on how to fix this issue. I've been searching around on Google for the past 3 hours.
Please help me solve this issue, this is due soon.. :(
Any and all help is greatly appreciated! :DLast edited by mpthegeneral; 03-19-2011 at 10:09 PM.
- 03-19-2011, 10:19 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
What is the exact runtime error message? And what line of your code does it refer to?
----------------
You can get more information about exactly where the problem occurs (both in terms of your code and in terms of the data file) by adding some diagnostic output.
Java Code:while(input.hasNext()) { String first = input.next(); String last = input.next(); String email = input.next(); String avail = input.next(); String skill = input.next(); String exp = input.next(); [color=blue]System.out.printf("Read %s %s %s %s %s %s%n", first, last, email, avail, skill, exp);[/color] if(input.hasNext()) { input.nextLine(); } TeamMember temp = new TeamMember(first, last, email, avail, skill, exp); addMember(temp); count++; }
- 03-19-2011, 10:26 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Oh, I'm sorry I forgot that. Here it is:
And its is referring to line 79 which isJava Code:Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at TeamDatabase.read(TeamDatabase.java:79) at CSTeam.dispatch(CSTeam.java:142) at CSTeam.main(CSTeam.java:53)
in the read method.Java Code:String email = input.next();
EDIT: Also I did the diagnostic output you suggested and received the following:
What does it mean?Java Code:Read Bob | Ma Read J o h n Read B i l l Read F r e d
Last edited by mpthegeneral; 03-19-2011 at 10:52 PM.
-
Myself, I would use a standard Scanner with the default delimiter to get each line in the file via:
and then would split each line using String#split(...) using "\\|" as the regex for splitting. I would use \\ to escape the | character since | has special meaning in regex.Java Code:while (input.hasNextLine) { String line = input.nextLine(); //... }
If you must use Scanner, then I'd use two Scanners, one to read in each line that uses the default delimiter (similar to above) and a second that works on each line obtained. I would still escape the | character the same: "\\|" for my Scanner delimiter.
- 03-19-2011, 10:42 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thank you but I am fairly new to this and I don't understand much of what you said. This program pretty much entails all of my current knowledge of Java; however, I did escape the delimeter and the new diagnostic output became this:
Does this mean it worked?Java Code:Read Bob Marley bm@yahoo.com Monday, Tuesday, Thursday before and after school Intermediate CompSci Read John Doe jd@yahoo.com Monday, Thursday after school Novice CompSci Read Billy Mays bm@yahoo.com Monday, Tuesday before school Expert CompSci Read Fred Flintstone fff@yahoo.com Monday, Tuesday, Thursday before and after school Newbie CompSci
*Sorry for all the edits..Last edited by mpthegeneral; 03-19-2011 at 10:52 PM.
- 03-19-2011, 11:07 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Does this mean it worked?
Hard to say without knowing the code... (did you stick with one scanner and escape the | when you set the delimiter?)
Anyway, where in the world is John Doe? Somehow or other "<newline>John" got included as part of the Bob Marley data. And then the rest of John Doe's data was lost.
I agree with foobarable. You are doing two things with the text in the file: splitting on newlines, then splitting on the pipe. To elaborate on his code:
Java Code:import java.io.StringReader; import java.util.Scanner; public class RecordReader { public static void main(String[] args) { // we can imitate reading from a file String data = "red|3|stop\ngreen|5|go\nblue|4|no value"; Scanner input = new Scanner(new StringReader(data)); while(input.hasNextLine()) { String line = input.nextLine(); String[] fields = line.split("\\|"); System.out.printf("Data read: %s %s %s%n", fields[0], fields[1], fields[2]); // now do whatever you like with the fields } } }
- 03-19-2011, 11:34 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Yes I did and it worked.... (did you stick with one scanner and escape the | when you set the delimiter?)
That was just an issue with my .txt file which I have fixed.Anyway, where in the world is John Doe?
I still don't understand much of the code you posted but thank you. I think my problem is now resolved! :D
Much thanks Fubarable and pbrockway2!! :)
Similar Threads
-
Newbie JCreator NoSuchElementException Help!
By Sly Cooper in forum JCreatorReplies: 2Last Post: 12-18-2012, 05:44 PM -
exception in thread main java.util.nosuchelementexception
By dude1it in forum New To JavaReplies: 6Last Post: 03-11-2011, 03:53 AM -
nosuchelementexception
By nicaskdjf in forum New To JavaReplies: 1Last Post: 02-24-2011, 05:14 AM -
Java Error: NoSuchElementException
By xpngamer in forum New To JavaReplies: 6Last Post: 03-19-2009, 07:37 PM -
[SOLVED] why am i getting this exception" java.util.NoSuchElementException
By ariz in forum New To JavaReplies: 5Last Post: 02-27-2009, 05:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks