0
How to get two string input and compare them in Java?
When I get two string input and compare them with two saved strings it always gives wrong result as I am using (== && ==) operators
12 Answers
+ 3
String is an object so it can't be compared using boolean operators like primitive data types
+ 7
Use equals() method if you want to compare there value and use instanceOf keyword if you want to compare there referance
+ 3
Use variable.equals(variable2). It is case sensitive, if you want to ignore case use .equalsIgnoreCase(variable2)
+ 2
No sorry I haven't used it often, but its System.exit(0). Look up "Java Docs" followed by a method, it's one of the best Java documentation I've found
+ 2
"string".equals("string")
>>
true
"string".equals("string2")
>>
false
+ 1
What about not equal !=
+ 1
Great
Thanks again
+ 1
You can't use a boolean operator when comparing contents of a string use the equals() instead. If it's case sensitive use the equalsIgnoreCase ().
+ 1
Thanks all I got my answer and could solve my problem using string.equals and !(string.equals())
+ 1
Hi, used the method equals()
0
No, instead you use !(var.equals()).
0
Thanks and do you know how to exit the program in java just like exit() function in c++