What's wrong with this Java Coded if STatement? [duplicate] - java

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am not a professional programer. I am still learning, so my code is a little basic right now.
Scanner UserInput = new Scanner(System.in);
String UserChoose = UserInput.next();
if (UserChoose=="Quit"){
I have deduced that there is something missing in the if statement, but I cannot figure out what. Can someone please tell me what I am missing? I have been searching online for an hour with no luck.

To compare objects in java use .equals() method instead of "==" operator
Replace if (UserChoose=="Quit"){ with if (UserChoose.equals("Quit")){

Common mistake, use UserChoose.equals("Quit") to compare strings.
Since String is an object, using == will likely compare the memory location the 2 strings or something that would always result with false.

if (UserChoose.equals("Quit")){
In java, the default == operator compare if they both are the same object, even if the content is the same, if the object reference is not the same, it isn't ==.
See this link for more complete explanation: http://blog.enrii.com/2006/03/15/java-string-equality-common-mistake/

Related

How to put a certain string into *if* and check if it is a certain word [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
I am trying to scan a string and put it in an if like so:
(this is just an example part of the whole program)
Scanner scan=new Scanner();
System.out.print("Enter the word...");
String a=scan.nextLine();
if (a=="Hi")
System.out.println("Hello!")
Else System.exit(0);
So im checking if the user said "Hi" , but it doesnt work like this, I want a way to do it.
I have just started learning java so my questions might seem a little amateur but your answers will help a lot.
Use "Hi".equals(a), == compares identity, not equality.
Also, you should write specifically "Hi".equals(a) and not a.equals("Hi"), because it won't cause a NullPointerException, since a might be null.

How to Compare two strings in android with optimization? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I already tried with string.equal function which will compare two strings, but what will be optimize way of doing same.
Suggestions are welcome.
Hi all got it may help others What's the quickest way to compare strings in Java?
Thanks Saurabh for sharing link.
According to me, You should use the equals() method of the String class to compare Strings.
if (xyz.equals("something")) {
//Do what you want if string is equal...
}
see this also..

Why can't I compare text from a TextArea to a String? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
So I am obviously missing something completely obvious here. I have a button and an actionlistener hooked up to it. When I click the button, I want run an if statement that takes the contents of a TextArea and compares it to a String. Like So:
String a = "hello";
JTextArea area = new JTextArea("type something");
JButton button = new JButton("Go");
button. [insert actionlistener crap]
//here's the important part:
if (area.getText() == "hello"){
//this will not be executed
}
It's really weird. I even went through the debugger and at that if statement, both of those items are "hello". But it skips over it! What am I doing wrong?
EDIT: a lot of you are saying "use .equals". Can anyone tell me why?
You should do area.getText(), this is a method, not a property.
Also you should compare them with equals not with ==.
So
"hello".equals(area.getText())
is the way to go.
You need to use String.equals() to compare the two. Not ==.
That should solve the problem.
You need compare String with help of method equals().
You compare strings in java with equals(), not ==.
As others are pointing out String.equals is needed.
The reason for this is that, == will check whether both objects are in fact the same object i.e. have the same memory address.

If statement getting true value [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
My program is using the following code
String mobile="";
if(mobile!="")
System.out.println("++++++");
System.out.println("------");
But if statement always set true. how it is possible? how to correct the code to get false for if condition?
try this way
if(!(mobile.equals("")))
there are difference between == and .equals()
As whoAmI has suggested its better to
if(!("".equals(mobile)))
because it can handle mobile as null
you can't use "!=" on strings. This only compares the references of the strings. If you want to compare the content of the strings you need to use equals
if(!("".equals(mobile)))

Comparing strings - Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I compare strings in Java?
I'm new to Java. And I've following problem:
string s = "someword";
if (s == "someword")
// do something
Sometimes doesn't work for me. Don't know why.
Thanks for responds.
In Java == compare reference. Use .equals() for compare value.
Duplication of this: How do I compare strings in Java?

Categories

Resources