String to bool (need help) | Sololearn: Learn to code for FREE!


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 ?

10/23/2021 11:28:10 AM


3 Answers

New Answer


Maybe you meant to use Convert.ToBoolean()?


bool a; String b = ...; a = b.Equals("true");


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#