| 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: 32599
|
|
Welcome to JavaRanch
Rob Prime points out it is completely possible to avoid the InputMismatchException like this, or something rather similar:
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
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: 19216
|
|
Campbell Ritchie wrote:Welcome to JavaRanch
Rob Prime 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
How To Ask Questions How To Answer Questions
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Maybe I miss something
|
|
|