How do you get user input for boolean....example, if a user is 18 years (true/ fals)...code for tht | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you get user input for boolean....example, if a user is 18 years (true/ fals)...code for tht

boolean

6th Jul 2017, 9:44 AM
Ian Ndiba
Ian Ndiba - avatar
6 Answers
0
@Ian so there is a function in java, Java.lang.Boolean.parseBoolean(string), which you could use. Though, I believe I'd recommend simply using a if-case for booleans. That works the same in pretty much any language as well. Also that allows you to parse something like "yes" and "no" to a bool. Remember to use toLowerCase to make it case-insensetive. A simple example: Scanner scanner = new Scanner(System.in); System.out.println("Are you 18 years or older? (Yes/No): ") ; String input = scanner.nextLine().toLowerCase() ; if (input == "yes") { // do stuff } else if (input == "no") { // do other stuff } else { // user entered something else than yes or no }
8th Jul 2017, 8:49 AM
LaserHydra
LaserHydra - avatar
+ 4
In what language?
6th Jul 2017, 10:02 AM
LaserHydra
LaserHydra - avatar
+ 1
If i got what you are asking: there is a code in c++. Hope it helps https://code.sololearn.com/ctUZHNr5PXyl/?ref=app
6th Jul 2017, 10:27 AM
Ledio Deda
Ledio Deda - avatar
0
in java
8th Jul 2017, 8:38 AM
Ian Ndiba
Ian Ndiba - avatar
0
thanks alot laser. You a life saver
9th Jul 2017, 5:58 AM
Ian Ndiba
Ian Ndiba - avatar
0
@Ian no problem mate
9th Jul 2017, 1:27 PM
LaserHydra
LaserHydra - avatar