• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird Error Message in Eclipse

 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

This is my first post at the saloon. I've been writing a program that puts data from a .dat file into a table that I formatted. It's been going smoothly so far but recently I have been getting this error message and I don't know what it could be from.

Here are the two classes that are involved with it.




and here is the text in the data file (employee.dat)...



Any help would be greatly appreciated.

Thanks,

n
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Have you looked up NoSuchElementException and Scanner? Notice what it says about when a Scanner can throw a NoSuchElementException.

You need to alter the for loop. Get a pencil and paper and count how many times it repeats.

Probably not a good idea to use a for-loop which depends on counting lines. There are methods in the Scanner class which tell you whether you can read a next token. They will fit nicely into a while loop, then you can read a file of any size.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the error message has nothing to do with Eclipse; it is from the Java Virtual Machine (JVM).
 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And would you look at that; I figured it out.

I deleted a line of the employee.dat file (making it so there's only 9 lines), and I had a loop that would run 10 times. So I just switched the line in my for loop from to .

I'm sure I'll be coming to the saloon more and more often.

Thanks
 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
Welcome to JavaRanch

Have you looked up NoSuchElementException and Scanner? Notice what it says about when a Scanner can throw a NoSuchElementException.

You need to alter the for loop. Get a pencil and paper and count how many times it repeats.

Probably not a good idea to use a for-loop which depends on counting lines. There are methods in the Scanner class which tell you whether you can read a next token. They will fit nicely into a while loop, then you can read a file of any size.



haha I just read your reply after I figured it out. Thanks for responding so soon.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

But try while (fileScan.hasNextLine()) . . .
It is much less error-prone than counting the lines.
 
marcus conway
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
You're welcome.

But try while (fileScan.hasNextLine()) . . .
It is much less error-prone than counting the lines.


Yeah, I know what you mean. But the problem is that I'm going to have to be reading each String (first name, last name, and salary) one at a time and invoke various methods on each string. So i kind of need the line to be broken up; I don't know how to separate a whole line into three different ones .hasNextLine().
 
Campbell Ritchie
Sheriff
Pie
Posts: 49361
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the Scanner API documentation, as in the link I posted yesterday!

I think the hasNextXXX() methods don't take any tokens from the input, so your next() should still work. But you will have to check the documentation.
I tend to have the opposite problem with Scanner; I find I need a "nextLine()" call every now and again to move onto the next line. But lots of experimentation and practice will soon get you up to speed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic