wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes What is causing this NoSuchElementException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is causing this NoSuchElementException?" Watch "What is causing this NoSuchElementException?" New topic
Author

What is causing this NoSuchElementException?

J Burns
Greenhorn

Joined: Oct 08, 2011
Posts: 6
I am teaching myself Java by working my way through a book. Part of the solution to one of its exercises is to fill an array from a text file. I was getting the above error so I wrote some test code. Can someone explain to me what I am doing wrong.



The output:
$ java Exercise6_4 < scores.txt
Score = 25
Score = 35
Score = 43
Score = 17
Score = 8
Score = 13
Score = 45
Score = -1
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Exercise6_4.main(Exercise6_4.java:11)

Thanks, Jim
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

NoSuchElementException will be throwed because the you are tring to read the data from the input (Scanner) where data is reached to the end.

So you have to read the input( Scanner ) by checking hasNext() is true or false


-Chiru
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I got an ArrayIndexOutOfBoundsException when I ran your code. Shouldn't you check only till scores[9] should be stored. When i becomes 10 the Exception pops up. Use a for loop instead of while.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
And as Chiranjeevi suggested since you have got the while loop in place you are going to hit by the NoSuchElementException all time you run the code even when you increment the input text file to have far more inputs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
And welcome to the Ranch
J Burns
Greenhorn

Joined: Oct 08, 2011
Posts: 6
Hey guys, thanks for the advice and the welcome.

Please don't think this reply is argumentative it's not meant to be, but while I was waiting I did some experimenting with the code I posted. The error goes away if instead of filling an array I just read in it's contents and print them out.



Output from code block (2):

$ java Exercise6_4 < scores.txt
Score = 25
Score = 35
Score = 43
Score = 17
Score = 8
Score = 13
Score = 45
Score = -1

Now I am curious why I seem to get the error only when I am dealing with an array. I am using a while loop instead of a for loop because of how the exercise is worded. It says I need to read in an unspecified number of scores but the maximum number possible was 10. It seemed the while loop was the one to use, was I wrong?

Thanks for your reply's, Jim
Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

Now I am curious why I seem to get the error only when I am dealing with an array.


Yes you can read only 10 beacuse
" int[] scores = new int[10]; "

You allocated the memory only for the 10 int, not more then that.

If you need to dynamic size array you have to use Collections, like ArrayList,Vectors
J Burns
Greenhorn

Joined: Oct 08, 2011
Posts: 6
Chiranjeevi Kanthraj wrote:

Yes you can read only 10 beacuse
" int[] scores = new int[10]; "

You allocated the memory only for the 10 int, not more then that.

If you need to dynamic size array you have to use Collections, like ArrayList,Vectors


Does this mean that Scanner tries to read past the end of the file multiple times? I ask because the file has 8 elements and as you point out the array has room for 10.

I just started the chapter on basic arrays so Collections etc are in the future. I figured Java must have some type of dynamic mechanism for allocation.

Thanks, Jim
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I reiterate...
John Jai wrote:I got an ArrayIndexOutOfBoundsException when I ran your code. Shouldn't you check only till scores[9] should be stored. When i becomes 10 the Exception pops up. Use a for loop instead of while.


The array has a room of 10 for sure. That means it can store from index 0 till 9. When you try to save in the index number 10 (literally your 11th element) you will encounter the error. Run below code and try to understand it.

J Burns
Greenhorn

Joined: Oct 08, 2011
Posts: 6
John Jai wrote:I reiterate...
John Jai wrote:I got an ArrayIndexOutOfBoundsException when I ran your code. Shouldn't you check only till scores[9] should be stored. When i becomes 10 the Exception pops up. Use a for loop instead of while.


The array has a room of 10 for sure. That means it can store from index 0 till 9. When you try to save in the index number 10 (literally your 11th element) you will encounter the error. Run below code and try to understand it.



All the times I have run the code I never got an ArrayIndexOutOfgBoundsException, it was always the error in the subject line.

I understand trying to store the 11th element would generate an exception. I don't think that is happening because the file I am reading in only has 8 elements. Am I thinking wrong here?

Anyway by putting { if (scores[i] < 0) break; } on line 17 of my code solved the problem.

Thanks for your and everyone else's help.

Regards, Jim
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Just a thought - if suppose 0 is the first element in the scores.txt file wouldn't the loop iteration stops after reading the first element? Wouldn't it be nice to iterate till the Scanner reads all the inputs from the text file? Check if any methods the Scanner class has that can help to do iterations till all the inputs are parsed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is causing this NoSuchElementException?