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 Antworten
+ 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;