• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is causing this NoSuchElementException?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
J Burns
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic