aspose file tools*
The moose likes Java in General and the fly likes Maybe I miss something Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Maybe I miss something" Watch "Maybe I miss something" New topic
Author

Maybe I miss something

Avi Freege
Greenhorn

Joined: Nov 13, 2009
Posts: 2
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.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Avi Freege
Greenhorn

Joined: Nov 13, 2009
Posts: 2
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
Welcome to JavaRanch

Rob Spoor points out it is completely possible to avoid the InputMismatchException like this, or something rather similar:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

Avi, welcome to JavaRanch.

Please use code tags when you post source code, so the forum can format it for you.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Maybe I miss something
 
Similar Threads
want some help for returning to main please
Initial variables before a try\catch block
How to alternate between two sets in a string?
Why we create our own Exception class ?
Head-Tail Counter