• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scanner

 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me when do I get a "java.util.NoSuchElementException" while using Scanner class ?

I was practicing few Scanner examples, and I am getting the above exception many times. The following is one example. But I am not able to figure the reason out. Please help.



The following is the output of the above code block :

Abc
7
false
3
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 ScanTest.main(ScanTest.java:27)


Its clear that the program contains int values in the String and the Scanner is able to identify that, thats why it prints the values in the output above. But when i say int a=scanner.nextInt(); it throws the exception.

Please explain.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Summet,


Can anyone explain me when do I get a "java.util.NoSuchElementException" while using Scanner class ?


there are many instances , anytime when there are no more input available to parse.


here the while loop , you have exhausted your scanner , so there are no more inputs available when executing scanner.nextInt() . nextInt() would throw NoSuchElementException exception , when it encounters no elements

HTH
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumeet ,
try this code and it would work




as balu said you reached the last element of the scanner and you don't have any next element to parse
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. Try this one:

 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balu for your reply.

But

here the while loop , you have exhausted your scanner


Can you please explain this.

I tried removing out the while loop , but still its throwing the same exception.



The output is :

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
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 ScanTest.main(ScanTest.java:22)


Kindly explain.


 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Morteza and Vinodkumar for your replies.

BTW Sorry, Its a different exception now. InputMismatchException. In the previous post I stated its the same exception.


I tried both Morteza and Vinodkumar's codes. Its giving the above exception at the line int age = scanner.nextInt();
Now why is this exception coming up ? I dont see any problem in that line .

And also please explain the concept of exhausting the scanner.

Thanks .

 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumeet ,
i think you are trying to put a string into int, change the code



and try it ,if you get the same error please post
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumeet,

java.util.InputMismatchException would occur when there is no matching Integer and still you try to parse it. Check the API first.

here the while loop , you have exhausted your scanner


here i meant scanner has parsed all your input.
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are getting following lines of error
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
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 ScanTest.main(ScanTest.java:22)

because you are trying to parse a String into an int

and regarding the concept of exhausting basically you are reaching the end of the scanner ,that is the last element in the while loop itself .
so you don't have any more elements to scan
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you ran my code, cause I remove that age variable! It runs perfectly and display appropiate results.
A long as you check for nextInt(), you can give any combination of string and int that you want to the scanner.
 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone for your replies. But please dont mind me asking a silly question like this. Its was really confusing me a lot.
But now I am getting closer to get the actual picture.

What I understood is .... if I check the existence of next int values using the while loop and print the value using scanner.nextInt() , it is working fine.



but if I use int age = scanner.nextInt(); , its throwing the exception.

The Java API says the return type of the nextInt() method is int. So in the above case where I am using the age variable, it should assign the first available int value from the String to the variable age. But its not doing that.

So my question is , what is the exact reason for the exception when I am using the line int age = scanner.nextInt();

This may sound silly, but believe me I am somehow missing a small trick to explain this program to myself. So kindly help.

Thanks
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sumeet
please modify your code as follows and try



you are trying to put the element in to an int with out checking weather its an int or not.
I think you will get it now
 
Sumeet Chakraborty
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vinodkumar. I got it.

Goodness me ... my mind was running on different track. I completely forgot that when the return type is unknown we have to check and cast it while assigning the value to a variable. I was thinking that ... if the int value exists in the String, then why not its getting assigned to the int variable.

Really it was silly from my side.

Thanks a ton everyone for your help.
 
Vinod Kumar Kommineni
Ranch Hand
Posts: 54
Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some times it happens.. silly mistakes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic