0
[JAVA] Easiest way to take ‘Y’ or ‘N’ as scanner inputs for boolean ?
Hi. I have a boolean scanner I’d like to take input as ‘Y’ for true and ‘N’ for false. Whats the best way to do this?
6 Answers
+ 4
//Here you go, it accepts upper and lower case character of Y theres no need for N because if no Y it's always N 👍
https://code.sololearn.com/c9JUpuU2dlek/?ref=app
+ 4
What behaviour should your program exhibit if the input is neither Y nor N?
+ 1
thanks bro D_Stark :)
0
I say take the input as char, then use a simple switch case to return a Boolean
0
unfortunately I can only use a predefined boolean attribute. Would it still work?
0
You cant input "Y" and expect the scanner to store true and assign it to as a boolean you need some conditional statements.
//I think this will work let me test it my self as I just wrote it...
Boolean sc = new Scanner(System.in).nextLine().equals("Y")?true:false;