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
}
Related
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);
}
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 4 years ago.
I am reasonably experienced with programming, but not specifically in Java. I am coming across the following error when working in eclipse. My code is the following:
I have used the debug function, and it reports that carbonPrefix is pent, but that carbon stays at 0 throughout. Like I said, I am a novice to Java and Eclipse, so I may not be using the debug function to it's full extent.
For anybody that's interested, this the start of code where you input the name of an alkane and it tells you the formula. It worked in Javascript and I'm just trying to translate it into Java.
Thank you all so much!
you have to use
carbonPrefix.equals("pent");
in java == operator used to compare two object references and the method equals() is used to compare two strings to determine whether they are equal or not.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
Consider a String variable pri with value 07:45:32PM , now in order to obtain the PM alone I applied,
pri = pri.replaceAll("[^A-Z]","");
So far things work fine, but trying to compare the value in the variable does not work, ie :
if(pri=="PM")
{
hh+=12;
}
The body of the loop does not get executed. My question is are the two values different, ie Pri=="PM" , Why is it so? And how do I get to check my if loop in a precise way? Thank you
EDIT1
So I tried if(pri.equals("PM")) instead of if(pri=="PM") , but still it did not solve the problem!
EDIT: Use "equalsIgnoreCase()" method for comparing strings.
if(pri.equalsIgnoreCase("PM"))
{
hh+=12;
}
You need to use .equals not == to compare two String Object.
if(pri.equals("PM"))
Use if(pri.equals("PM")) instead.
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/