[java] Simple boolean confusion

Started by
5 comments, last by Sir_Spritely 21 years, 5 months ago
What I am trying to is very simple, or so I thought, While boolean variable WCheck = True Prompt user if they are happy with display Y or N Read in answer as string IF answer = Y THEN Set boolean variable Check to True Set boolean variable WCheck to False ELSE IF answer = y THEN Set boolean variable Check to True Set boolean variable WCheck to False ELSE IF answer = N THEN Set boolean variable Check to False Set boolean variable WCheck to False ELSE IF answer = n THEN Set boolean variable Check to False Set boolean variable WCheck to False ELSE Display "Please enter Y or N only !" ENDIF END WHILE The problem I am getting is, C:\Program Files\Xinox Software\JCreator LE\MyProjects\Java1\ALARM\Initial_Setup.java:54: incompatible types found : java.lang.String required: boolean if (yncheck = "N") ^ Also as well is this the best method to cope with a situation such as this or is there a more effecient way? Thanks, Paul.
Advertisement
yncheck = "N" doesnt evaluate to a boolean, it is an assignment.
Use the == operator for comparisions. But for strings i would suggest using the equals string member function.
I have now got it to compile, I cannot believe I still make those stupid assignment and equals to errors.

Anyway What I am getting now is the program will not accept anything and just keeps looping.

The actual code I am using is; -

System.out.println ("Is there a garage or shed included in the system Y/N ");
yncheck = in.readLine();
if (yncheck == "N")
{
shed = false;
infocheck = false;
}

This is the only way I know how to perform this, anyone know the correct way? A detailed explanation always helps

Thanks,

Pk
quote:Original post by Sir_Spritely
if (yncheck == "N")


Noooooo! You cannot compare Strings in this way, you''re not comparing string content, butt the memory addresses of the String objects. While this may work sometimes (due to JVM immutable string memory management funkiness) its deeply unstable. Use if (ynCheck.equals("N") ) to perform string comparison properly.

You should really show your whole code. If ''in'' is an InputStream, there is no such thing as readLine() for a basic InputStream. The way to get input from the console is to use ''System.in''. So if you created your ''in'' like this:

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

then it should work.

Post the whole code. It makes it easier to spot your error.


Make it work, then
make it fast.

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
OrangyTang is right. the == operator is undefined for string comparison.

instead of:

whatever == "N"

u MUST use:

nameOfFirstString.equals(nameOfSecondString) ;

GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
Hey Sir_Spiritely,

if you do not provide the whole code, we can''t help you much.

just FYI, there are MORE member functions of the String class, than just "equals". Spend some time reading the API documentation - it''s in HTML and that kind of knowledge tends to pay off in the long run (for programmers).

And to your little problem ...

You can use "equalsIgnoreCase" to compare Strings and ignore the case (so the program accepts not only "Y" and "N", but also "y" and "n").

For example ...

String
s1 = "Hello",
s2 = "hello";

while (s1.equals(s2)) {
// this does not loop a single time - the "H" is once a capital "H" and once a lowercase "h"
}

while (s1.equalsIgnoreCase(s2)) {
// this loops infinitely - ignoring the case of the letters, the two words are identical!
}


P.S.: If you did not read the book "Thinking in Java" (by Bruce Eckel), do so. It explains the very basic concepts of OOP in Java and also takes you through the basic Java libraries with wonderfull working examples .

what a long post again ... sorry

Petr Stedry
Petr Stedry

This topic is closed to new replies.

Advertisement