#urgent, boolean input validation
18 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @quiet anchor! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
you already are validating boolean only input, you're not enforcing it, which isn't possible given the environment of the console.
instead, catch the error and loop to reprompt the user.
also, consider
a) prompting with true and false instead of True and False
b) prompting for y/n instead
okay so, im fairly new to Java and in an attempt to "catch" the error following user input I included this if statement:
if (reserved != true || false) {
System.out.println("Incorrect answer format please try again. Answer must be either True or False.");
reserved = scnr.nextBoolean();
}
it doesnt work, any suggestopm
that doesn't catch any error at all
maybe uh. don't guess what i mean when i say "catch the error"
if you don't understand it, say so.
okay, thanks for the rude comment. i came here for help not a smart ass.
reserved != true || false is just checking that reserved is false.
the "correct" way to write this would be reserved != true && reserved != false, fyi, but this is kinda a useless statement
anyways, check here for how exceptions work:
https://dev.java/learn/exceptions
when you input some invalid boolean, the scnr.nextBoolean() will have given you an InputMismatchException, and you need to catch that, there's a special way to do it explained there ^
you would need to loop to keep reprompting if the user continues inputting nonbooleans
thanks for the actual advice, now for my bit of advice.. when someone who states they need help (aka not a professional or perfect) that means if you're going to comment. help. don't criticize. if i was asking for critiques i would have said so. you will go nowhere in life with the attitude you brought previous to the actual help. all you had to say was "that doesnt catch any error at all, heres a link i think could help you". now we've both learned something today, crazy!
i literally did give you advice though
goodbye