This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
So, I'm trying to do something really simple, and that's check if a password equals something in my SWT application. So, my code was this:
if (passwordBox.getText() == "test") {
passwordBox.setVisible(false);
}
However, in my application, which uses text fields marked as passwords (with that variable), it will not fire that when I click a button. I already have button handling working, since I have an else statement that fires, but this will not fire when the password box obviously equals "test". What's wrong here?
Strings are always compared by equals().
if(("test").equals(passwordBox.getText())) {
passwordBox.setVisible(false);
}
Related
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.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i'm taking user input from
String riddle=b.readLine();
riddle1=Integer.parseInt(riddle);
and obviously, i'm trying to get the user to answer a riddle.
I tried using the following
if(riddle1=="answer")
but that doesn't work
Try this
riddle.equals("answer");
As == doesn't checks whether both strings are equal or not, it compares string's location.
You can click here for more information about it.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I have been trying to connect to a mysql database and it was succesfull. In the onPostExecute method, I get a result which seems to me exactly the same as the real input. In this example the emailadress jb#jb.jb. Logcat shows both the real input as the result. Yet my code doesn't agree that it is the same. Any advice?
EDIT: This link apparently gave the answer: answer on the question
You have to use String.equals() instead of == to compare Strings. check this link
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
EDIT: Thank you, PakkuDon
instead of using "==" I must use ".Equals()"!
I'm trying to implement a chat-command system though I've encountered troubles in differentiating between standard chat and command chat..
Focusing on just one command for now, `highlight
the output whenever I type in `highlight is:
highlight
`highlight
Here's my code:
String cmd = InMessage.message.substring(0, 10);
System.out.println(cmd);
System.out.println("`highlight");
if( cmd == "`highlight" )
{
... cancel chat packet and proces command
}
and yet the if statement returns false.
What's going on here? Anything I've done is wrong?
You have a number of problems. Firstly, that substring is going to throw an exception if the user types something that's less than 10 characters. Secondly, you're using == where equals would work better. But you don't need either of these things. You could just use
if (InMessage.message.startsWith("`highlight")) {
// whatever
}
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)))