regex java, unable to get the value [duplicate] - java

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.

Related

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..

Comparing strings in onPostExecute (resulting from mysql operation) [duplicate]

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

Java substring issue [duplicate]

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
}

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

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/

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)))

Categories

Resources