The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

String or Boolean help

Ok, so i hope i am not going against any rules by posting here... i read and re-read the rules, so if i accidently break one, please let me konw.

I did search google and stuff for this answer, but none of them post or tutorials i watched showed me what i needed.

Ok, so i am trying to make a class (program?) that takes what the user inputs and replies to it.

so i have

importjava.util.Scanner;//so i can use the scannerpublicclass ScannerMain {publicstaticvoid main (String args[]){
Scanner wordscanner =new Scanner(System.in);//setting the scannerboolean con =true;//i am just using this to keep the string repeating after they input, so they can reply to what the computer outputswhile(con =true){String answer;System.out.print("Reply:");
answer = wordscanner.next();//mostly self explanitoryif(answer ="hi"){/*here is where i get the error.... it says unable to convert String to boolean i tried changed the answer to a boolean, or copying the value of the boolean to a new variable.. but that didn't work. also, i think i may have to word the "hi" differently, what im trying to do with that: is seeing if the user had wrote hi, and if so reply with hello*/System.out.println("Hello");}else{System.out.println("I do not reconize your statement");}}}}

Again, i did try converting the string to a boolean, but i couldn't seem to get that to work.

Code Without any comments (so you might be able to read the actual code better)

Re: String or Boolean help

one = is 'assignment', it means copy the value on the right to the variable on the left. The type of an 'x = y' expression is the type of x, so String x = "a letter y" is an expression with a String type.
two =, like == is 'test for equality', compare the left side to the right side and return boolean true if they're the same, boolean false otherwise. It doesn't matter what type x and y are in x == y, the type of the expression is boolean.
If statements are if ([boolean_expression]) - no other type of expression is allowed.

Re: String or Boolean help

Originally Posted by Sean4u

one = is 'assignment', it means copy the value on the right to the variable on the left. The type of an 'x = y' expression is the type of x, so String x = "a letter y" is an expression with a String type.
two =, like == is 'test for equality', compare the left side to the right side and return boolean true if they're the same, boolean false otherwise. It doesn't matter what type x and y are in x == y, the type of the expression is boolean.
If statements are if ([boolean_expression]) - no other type of expression is allowed.

i am ashamed, i have watched so many tutorials, and actually used that before (the ==) my brain was out of it, thank you

ok, now i don't know if this is the way i worded the code or not, but no-matter what i type, it still goes with the second argument (I do not reconize that statement)

Re: String or Boolean help

== compares the left hand side with the right hand side. answer's 'type' is a String reference - it's a reference that you assigned to a (reference to a) String object returned from your Scanner's next() method. "hi" is another, completely separate String object which has its own reference. The two references are not the same. '==' is doing the very shallowest of comparisons between the two reference types. It is asking "is the left hand side referring to the same object that the right hand side is referring to?". If you want to do something deeper (like compare the content of the two objects to see if they hold the same sequence of characters), you'll need to use the equals() method. Read the API documentation for Object.equals and String.equals

Re: String or Boolean help

Keep in mind that the == operator will only work for primitive data types (char, int, double, float...ect.). When comparing Objects, in this case Strings, you should use the equals() method provided by the Object class.