Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is wrong with this code?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am not getting from this as I expect. Once exception comes, it is not
asking input from console. Only printing Line 1 consecutively, (remaining counts of outer loop)




Thanks,
cmbhatt
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the catch block, you need to call nextLine() to skip the erroneous input.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Satou,


In the catch block, you need to call nextLine() to skip the erroneous input.


That works fine, but question arises why so!
Please clarify!

Thanks,
cmbhatt
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer in the API :
nextInt():
An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix)

nextInt(radix):
This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched


So you'll have to advance past the incorrect input if you want to continue scanning for an int.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If the translation is successful, the scanner advances past the input that matched.


It means scan.nextInt(); fails to translate what is given to it, "a" will consist the previous value, just before succeeded input. That is correct
as I did so.

But how can be corelate this thing with what happened with my code?
and when I use nextLine() in the catch clause why doesn't it ask for input.
It means if previous nextInt() fails to translate, the further nextLine()
will only advance automatically, without asking input.???

Thanks,
cmbhatt
[ April 25, 2007: Message edited by: Chandra Bhatt ]
 
Kumar Sushil
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its workin fine......no exception.
 
Kumar Sushil
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its working fine......no exception.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

After adding Satou's nextLine now it working fine;
Here is code:


Here is the output:
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Satou and Srini,


Thanks and Regards,
cmbhatt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic