1

So i have to ask the user whether they are above 18 and they have to answer with a true or false. And keep looping until they enter the right input

So far, i have this

boolean b = false;
do {
    try {
        System.out.print("Are you above 18?");
        Scanner n = new Scanner(System.in);
        boolean bn = s.nextBoolean();

        if (bn == true) {
            // do stuff
        } else if (bn == false) {
            // do stuff
        }

    } catch (InputMismatchException e) {
        System.out.println("Invalid input!");
    }
} while (!b);

HOWEVER, it wont work as the loop keeps going and it wont read the input right and do my if statements. How do i fix this? Thanks!

5
  • I will go with y/n option in this case. Commented May 15, 2014 at 0:46
  • bn == true is redundant. bn evaluates to a boolean value. Commented May 15, 2014 at 0:47
  • 4
    Are you setting your b variable to true inside one of your conditions? Your loop won't stop until b is true and it's not reference anywhere in your loop. Did you mean to do while (!bn)?. Commented May 15, 2014 at 0:47
  • Just off the topic, replace your if(bn == true) with if(bn). Commented May 15, 2014 at 0:48
  • What input values have you tried? Commented May 15, 2014 at 0:48

4 Answers 4

5

slight tweak to your program. This works

boolean b = false;
        do {
            try {
                System.out.print("Are you above 18?");
                Scanner n = new Scanner(System.in);
                boolean bn = n.nextBoolean();
                if (bn == true) {
                    System.out.println("Over 18");
                } else if (bn == false) {
                    System.out.println("under 18");
                }

            } catch (InputMismatchException e) {
                System.out.println("Invalid input!");
            }
        } while (!b);

And the output is

Are you above 18?true
Over 18
Are you above 18?false
under 18
Are you above 18?
Sign up to request clarification or add additional context in comments.

1 Comment

Just formatted it. Indentations were little off. :) Not sure if that's what was your question?
2

You need to re-check your Scanner statement and Initializing statement...

Scanner n = new Scanner(System.in);
boolean bn = s.nextBoolean();

It should be

Scanner n = new Scanner(System.in);
boolean bn = n.nextBoolean();  

Comments

0

Try changing

boolean bn = s.nextBoolean();

to

b = s.nextBoolean();

Comments

0

Use-

scanner.nextBoolean()

    do{
      try{
          System.out.print("Are you above 18?");
          Scanner scanner = new Scanner(System.in);
          if(scanner.nextBoolean()==true)
           //do stuff

          }else{

          //do stuff
          }
       }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.