String to bool (need help) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

String to bool (need help)

I would like to get user Input true or false, but i don't know how to do it. And Convert.ToBool doesn't work. Some ideas ?

23rd Oct 2021, 11:28 AM
Pacifik
Pacifik - avatar
3 Answers
+ 4
23rd Oct 2021, 2:53 PM
Ipang
+ 3
bool a; String b = ...; a = b.Equals("true");
23rd Oct 2021, 11:52 AM
Mr. EDROL (ru)
Mr. EDROL (ru) - avatar
+ 1
String st = args[0]; if(st.compareTo(”true”)==0 || st.compareTo(”false”)==0) { boolean b=Boolean.parseBoolean(st); System.out.println(”” + b); } else System.out.println(”not a boolean value”); } The above program is considering strings If the string is either true or false It will be converted to boolean and printed it Otherwise, it is printing a warning message Not a boolean value This code is as per java which is similar to c#
25th Oct 2021, 9:10 AM
sree harsha
sree harsha - avatar