• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

What is wrong with this code?

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its workin fine......no exception.
 
Kumar Sushil
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its working fine......no exception.
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satou and Srini,


Thanks and Regards,
cmbhatt
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic