Results 1 to 5 of 5
Thread: What does this error mean?
- 01-25-2011, 06:18 AM #1
Member
- Join Date
- Jan 2011
- Location
- Oregon
- Posts
- 4
- Rep Power
- 0
What does this error mean?
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at phonebook.main(phonebook.java:69)
Press any key to continue . . .
And these are the chunks of code I am using:
Java Code:import java.util.Scanner; import java.io.*; public class phonebook { public static void main (String[] args) throws IOException { String first, last, address, city, state, zipcode, number, hold; int zipint; Scanner filer, disector; pages entries = new pages (); filer = new Scanner (new File("phonebook.txt")); while (filer.hasNext()) { hold = filer.nextLine(); disector = new Scanner (hold); disector.useDelimiter("\n"); while (disector.hasNext()) { first = disector.next(); last = disector.next(); address = disector.next(); city = disector.next(); state = disector.next(); zipcode = disector.next(); number = disector.next(); zipint = Integer.parseInt(zipcode); entries.addEntry (last, first, address, city, state, number, zipint); } } System.out.println (entries); }}Java Code:public class bookentry { private String First, Last, Address, City, State, Num; private int Zipint; public bookentry (String first, String last, String address, String city, String state, String num, int zip) { First = first; Last = last; Address = address; City = city; State = state; Num = num; Zipint = zip; } }Java Code:public class pages { private bookentry[] bookpages; private int count; public pages () { bookpages = new bookentry[25]; count = 0; } public void addEntry (String first, String last, String address, String city, String state, String num, int zip) { if (count == bookpages.length) increaseSize(); bookpages[count] = new bookentry (first, last, address, city, state, num, zip); count++; } private void increaseSize () { bookentry[] temp = new bookentry[bookpages.length * 2]; for (int potato = 0; potato < bookpages.length; potato++) temp[potato] = bookpages[potato]; bookpages = temp; } public String toString () { String endgame = ""; for (int counter = 0; counter < count; counter++) { endgame += bookpages[counter].toString() + "\n"; } return endgame; } }
and the text file composed of randomized data
Cecil \n Harding \n 1453 Short Street \n Adams \n Oregon \n 97810 \n (541)987-2556
Myra \n Gray \n 1815 Coleman \n Avenue \n Adair Village \n Oregon \n 97330 \n ( 541)995-9849
Trina \n Lewis \n 1834 Byers \n Lane \n Adrian \n Oregon \n 97901 \n (541)948-6587
Damion \n Carver \n 194 Frum Street \n Albany \n Oregon \n 97321 \n (541)344-4814
Darren \n Weiss \n 4627 Sherwood Circle \n Amity \n Oregon \n 97101 \n (503)956-9501
Cristina \n Fuller \n 4835 Boone Street \n Antelope \n Oregon \n 97001 \n (541)489-8468
Dale \n George \n 2739 Findley Avenue \n Arlington \n Oregon \n 97812 \n (541)894-0959
Ann \n Oliver \n 2585 Ben Street \n Ashland \n Oregon \n 97520 \n (541)845-0661
Clinton \n Eaton \n 3217 College View \n Astoria \n Oregon \n 97520 \n (503)369-5198
Jennifer \n Webster \n 1211 Heavner Court \n Athena \n Oregon \n 97813 \n (541)864-9463
Graham \n Albert \n 4908 Finwood Road \n Aumsville \n Oregon \n 97325 \n (503)378-6187
Hunter \n Burgess \n 1460 High Meadow Lane \n Aurora \n Oregon \n 97002 \n (503)338-0919
Raquel \n Durham \n 4743 Masonic Drive \n Baker City \n Oregon \n 97814 \n (541)136-4704
Annette \n Wall \n 462 Post Farm Road \n Bandon \n Oregon \n 97411 \n (541)952-5027
Gary \n Hebert \n 2983 Alexander Avenue \n Banks \n Oregon \n 97106 \n (503)937-3066
Alfonzo \n Holden \n 440 Williams Lane \n Barlow \n Oregon \n 97013 \n (503)968-7302
Loraine \n Cole \n 2819 Melville Street \n Bay City \n Oregon \n 97107 \n (503)931-4537
Selena \n Nicholson \n 4519 Reynolds Alley \n Beaverton \n Oregon \n 97000 \n (503)914-5970
Gloria \n Hatfield \n 4472 Ventura Drive \n Bend \n Oregon \n 97701 \n (541)935-9867
Mariano \n Buck \n 2940 Clousson Road \n Boardman \n Oregon \n 97818 \n (541)939-9043
Cherie \n Bird \n 13 Davis Avenue \n Bonanza \n Oregon \n 97623 \n (541)927-4312
Roland \n Dixon \n 3285 Brown Avenue \n Brookings \n Oregon \n 97415 \n (541)912-8992
Georgina \n Bowen \n 1727 Parker Drive \n Brownsville \n Oregon \n 97327 \n (541)973-9431
Alfonso \n Noel \n 985 Caynor Circle \n Burns \n Oregon \n 97720 \n (541)968-8086
Jaime \n Buck \n 3211 Green Avenue \n Butte Falls \n Oregon \n 97522 \n (541)991-3068Last edited by DarrylBurke; 05-13-2012 at 11:54 AM. Reason: resolved
- 01-25-2011, 07:15 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1347)
at phonebook.main(phonebook.java:69)
This stack trace is indicating that on line 69 of phonebook.java you are calling the scanner's next() method. But the Scanner instance doesn't have a next element at that time. It would help if you said which line was line 69.
Part of the problem may be the following:
Java Code:disector.useDelimiter("\n");
Notice that the string "\n" has a single character (the newline character). But in your data file you separate the fields with a string of four characters (space, backslash, n, space).
- 01-25-2011, 07:41 AM #3
Member
- Join Date
- Jan 2011
- Location
- Oregon
- Posts
- 4
- Rep Power
- 0
Ok, I decided to change out the \n to using tabs and I found where I had one too many \n in my text lines before. I completely blame my instructor who thought it was a good idea to use \n for this project.
Thank you for helping me find my error.
Edit: Now I just have to get my toString to work instead of my program printing addresses.Last edited by Chrizesu; 01-25-2011 at 07:44 AM.
- 01-25-2011, 08:02 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Great.
I guess you mean adding a toString() to the book entry class. That shouldn't be hard: but post if anything goes wrong.
One little (but important) thing is that you should use standard Java naming conventions. Classes start with a capital letter and variables with lowercase. So Bookentry (or BookEntry), first, last, zipInt etc.
- 01-25-2011, 08:20 AM #5
Member
- Join Date
- Jan 2011
- Location
- Oregon
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
java out of memory error-heap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06-15-2010, 09:31 AM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks


Bookmarks