• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java Help... Converting String to Boolean

Krueger81

Diamond Member
OK I have been wrestling with this for the past couple of hours.

Here is the code I have:

System.out.print("The squeaky wheel gets the grease. Please enter True or False.");
s1 = br.readLine();

if (s1 = 'true')
counter++;

Obviously there is more to it and then all works

I need to be able to take the value that I typed in and convert it to a boolean somehow so I can take it and compare it so I can add one to the counter if its true or don't add if its false. I only started this class 6 weeks ago so don't yell about my java skills 🙂

Please send me a PM for the problem and the complete code
 
if(s1.equals("true")) { counter++; }

Post in the software forum next time, it doesn't get enough traffic.
 
Well if you cant just do the if(s1.equalsIgnoreCase("true")) statement, create a boolean variable (for example: c)

boolean c = s1.equalsIgnoreCase("true");
If s1 is true, c will be true.
If s1 is false, c will be false.
 
Originally posted by: Krueger81
Here is the code I have:

System.out.print("The squeaky wheel gets the grease. Please enter True or False.");
s1 = br.readLine();

if (s1 = 'true')
counter++;

The 2 posters above me gave you solid solutions, but I'll point out one thing that will be important for you to know for the rest of your class. When you want to compare two things like you are doing in your if statement, you want to say: if ( s1 == 'true'). The reason is because = is the assignment operator meaning it is used to assign values to variables while == is the comparison operator which is used for comparing two values. It's a minor, but very important distinction.

-Tom
 
Back
Top