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!
y/noption in this case.bn == trueis redundant.bnevaluates to a boolean value.bvariable totrueinside one of your conditions? Your loop won't stop untilbistrueand it's not reference anywhere in your loop. Did you mean to do while(!bn)?.if(bn == true)withif(bn).