1
boolean z = false;
do {
    try {
        a = sc.nextInt();
        z = true;
    }
    catch(Exception e) {
    }
}
while(!z);

Try this. If you try an integer the first time it executes properly. However if you enter the wrong type of text it turns into an infinite loop even if you enter an int next and skips assigning the boolean value to true. Why is this?

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • 7
    This code makes little sense. There's no variable catcher declared and it's certainly not changed within the loop. Perhaps you copied it wrong? For problems like this, it's always best to post a small self-contained compilable program as much as possible. – Hovercraft Full Of Eels Jan 27 '11 at 03:20
  • 2
    Is this line supposed to be while(z == false); ??? – CoolBeans Jan 27 '11 at 03:23
  • I'm guessing yes, but even that is bad form. Better is while (!z); – Hovercraft Full Of Eels Jan 27 '11 at 03:25
  • @DasWood in your `catch(Exception e){}` section, make sure to write `e.printStackTrace()`. It's usually really bad to swallow exceptions without reporting what went wrong. The stack trace will provide you the line number where things are failing. – Jonathon Faust Jan 27 '11 at 03:25
  • I don't think you need to print a stack trace here since here the exception is not quite as "exceptional" but rather a way to branch program logic. Better is to have a finally block (I think) as per my answer below. – Hovercraft Full Of Eels Jan 27 '11 at 03:29
  • @CoolBeans Yes it is supposed to be z==false. It was something I missed when renaming variables before posting it here. @Jonathon I removed the stuff in it because it didn't make a difference to if it looped or not. –  Jan 27 '11 at 03:30

1 Answers1

8

Your problem is from not handling the end of line token and so the scanner is left hanging. You want to do something like so:

import java.util.Scanner;

public class Foo2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = 0;
        boolean catcher = false;
        do {
            try {
                System.out.print("Enter a number: ");
                a = sc.nextInt();
                catcher = true;
            } catch (Exception e) {
            } finally {
                sc.nextLine();
            }

        }
        // !!while(catcher == false);
        while (!catcher);

        System.out.println("a is: " + a);
    }
}

Also, while (z == false) is bad form. You're much better off with while (!z). This prevents the while (z = false) error, and is a cleaner way of expressing this.

edit for Marcelo:

Marcelo, thanks for your input and advice! Are you saying that the conditional in the if block below will not change the value of the boolean, spam?

  boolean spam = true;

  if (spam = false) {
     System.out.println("spam = false");
  }

  System.out.printf("spam = %b%n", spam);

Because if it does change it, the coder wouldn't expect this if they intended to write if (spam == false), then there could be subtle and dangerous side effects from this. Again, thanks for helping to clarify this for me!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks. The finally worked perfectly. That said I thought !z and z== false were the same thing? –  Jan 27 '11 at 03:33
  • They are, but most feel that !z is cleaner. And again, you don't run into the pernicious if (z = false) error where the if condition actually sets the boolean. – Hovercraft Full Of Eels Jan 27 '11 at 03:35
  • 1
    The assignment error won't happen in Java because assignments don't evaluate to any value (they're not valid expressions. That is a problem from C, C++ and other languages. – Marcelo Jan 27 '11 at 04:29
  • 1
    Marcelo, please see edit to my post above. Thanks in advance for any advice and insights you can give me! I'm still learning this great language. – Hovercraft Full Of Eels Jan 27 '11 at 04:56