How to get two string input and compare them in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

24th Feb 2020, 2:01 AM
Life Lessons
Life Lessons - avatar
12 Answers
+ 3
String is an object so it can't be compared using boolean operators like primitive data types
24th Feb 2020, 2:15 AM
Odyel
Odyel - avatar
+ 7
Use equals() method if you want to compare there value and use instanceOf keyword if you want to compare there referance
24th Feb 2020, 3:05 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 3
Use variable.equals(variable2). It is case sensitive, if you want to ignore case use .equalsIgnoreCase(variable2)
24th Feb 2020, 2:09 AM
Odyel
Odyel - avatar
+ 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
24th Feb 2020, 2:17 AM
Odyel
Odyel - avatar
+ 2
"string".equals("string") >> true "string".equals("string2") >> false
24th Feb 2020, 1:04 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
What about not equal !=
24th Feb 2020, 2:10 AM
Life Lessons
Life Lessons - avatar
+ 1
Great Thanks again
24th Feb 2020, 2:19 AM
Life Lessons
Life Lessons - avatar
+ 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 ().
24th Feb 2020, 9:08 PM
Vanessa
Vanessa - avatar
+ 1
Thanks all I got my answer and could solve my problem using string.equals and !(string.equals())
25th Feb 2020, 1:53 AM
Life Lessons
Life Lessons - avatar
+ 1
Hi, used the method equals()
26th Feb 2020, 12:47 AM
Gabriel Díaz
Gabriel Díaz - avatar
0
No, instead you use !(var.equals()).
24th Feb 2020, 2:14 AM
Odyel
Odyel - avatar
0
Thanks and do you know how to exit the program in java just like exit() function in c++
24th Feb 2020, 2:15 AM
Life Lessons
Life Lessons - avatar