• 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

Maybe I miss something

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code:

while (LegalSlot == 0) {
if (gslots[anser].getplaya() > 0) {
gslots[anser].removplayer(1);
if (gslots[anser+numcube].getladderRope() == 0) {
gslots[anser+numcube].addplayer(1);
} else {
gslots[gslots[anser+numcube].getladderRope()].addplayer(1);

}
flag++;
LegalSlot = 1;
} else {
System.out.println("Ilegal Slot Pick Again;");
try {
anser = (input.nextInt())-1;
} catch (InputMismatchException e) {

}

}
Now I want to catch the exception if the user enters char such has 'a' 'b' and anything other than int , but when he catches the exception I want him to come back to the line : anser = (input.nextInt())-1;
to recieve new input from the user but somehow he just keep going threw the code. So my question is how do I make him come back to the the input line I mentioned?
thanks.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Avi Freege
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried that for some reason he is going back to the start of the loop and completely ignores the raw:

try {
anser = (input.nextInt()) - 1;
} catch (InputMismatchException e) {
System.out.println("Please Try Again");

thats gets an input from the user and I get the loop keep going and going
I debuged it and I dont understand why when he reaches the raw
anser = (input.nextInt()) - 1;
he is not stopping and waiting for an input
hope someone can explain it to me.
but thanks.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Rob Spoor points out it is completely possible to avoid the InputMismatchException like this, or something rather similar:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avi, welcome to JavaRanch.

Please use code tags when you post source code, so the forum can format it for you.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to JavaRanch

Rob Spoor points out it is completely possible to avoid the InputMismatchException like this, or something rather similar:


Correct. The input.next() inside the method is used to consume the non-int. The guard is slightly wrong though; it should also check for input.hasNext:
I think I forgot the hasNext() before in the loop, but that could cause an exception with the input.next() call if the stream is closed.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic