This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I have a String response_content of length=1 in a java project and I know that it equals to 1 (ASCII 49). I know this, because the following lines (in Eclipse with android ADT+SDK)
Log.i("GET RESPONSE", response_content);
Log.i("GET RESPONSE", response_content.length());
Log.i("GET RESPONSE", response_content.codePointAt(0));
produce this output:
1
1
49
But why do these lines always return false?
if (response_content.equals(1)) {...}
if (response_content == "1") {...}
I know equals() is the adequate way, == is just for testing purposes.
Is there another way of telling me, what the string really contains or is there a mistake I don't see?
The first one
if (response_content.equals(1)) {...}
is comparing a String object to an Integer object ... they don't match on object-type, so this evaluates false. The second one
if (response_content == "1") {...}
is comparing the reference, and not the data itself ... 'response_content' is a separate object from "1", so this evaluates false.
Try this instead:
if (response_content.equals("1")) {...}
must be response_content.equals("1") otherwise a String object gets compared to an Integer object
response_content == "1" may return true but doesn't have to because those can be different "1" strings.
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I'm trying to make a dutch zipcode validator. On the first step i need to check if the first char of the zipcode (which is always a number), is between 1-9. But when i just tried to make a simple if statement to check if the first char is false, it comes back as true. In this example I took a premade string "2097AR" as input for the method "checkPostcode".
public static boolean checkPostcode(String postcode){
String postcodeEen = postcode.substring(0,1);
boolean resultCheck = true;
System.out.println(postcodeEen);
if (postcodeEen == "1"){
resultCheck = false;
}
return resultCheck;
}
The = operator for strings in Java checks if the two objects are the same, whereas #equals(String s) checks if the contents of the string are the same.
The function always returns true because postcodeEen and "1" aren't the same object, which makes the condition postcodeEen == "1" false.
Here is the working code segment:
if(postcodeEen.equals("1"){
return false;
}
This question already has answers here:
Integer wrapper class and == operator - where is behavior specified? [duplicate]
(2 answers)
Closed 7 years ago.
public void pop() {
int a = stack.peek();
int b = min.get(min.size()-1);
System.out.println("a:"+a+" "+"b:"+b);
if (a==b) {
System.out.println("111");
}
if (stack.peek()==min.get(min.size()-1)) {
System.out.println("222");
}
stack.pop();
}
I created a class called MinStack, here is the pop(), the variable stack is a Stack(Integer), and min is an ArrayList(Integer), but the second if stmt is not always working correctly.
I got console like this:
a:512 b:-1000
a:-1000 b:-1000
111
a:-1000 b:-1000
111
I think the "111" and "222" will always show together, but here is not.
If I change the second if stmt as stack.peek()-min.get(min.size()-1)==0, then it works correctly, why this happened?
Thanks in advance.
EDIT:
I know where is wrong, I have to use stack.peek().intValue()==min.get(min.size()-1).intValue(), cause they are Integer.
This is an issue of Integer comparison. stack.peek and min.get(min.size()-1) both return Integers. Even though they contain the same int value, they are not the same Integer instance, so the comparison returns false.
Change your code to :
if (stack.peek().equals(min.get(min.size()-1))) {
System.out.println("222");
}
The alternative comparison stack.peek()-min.get(min.size()-1)==0 returns true because here you are comparing two int primitives.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I wanna ask you ,
do you have a solution for this question (if statement) or where is the error ?
String z = input.nextLine() ; //i want from the user value of z .
if (z ==( "Y" || "y" ))
{
statement ...
}
else if (z==("N" || "n" ))
{
statement ...
}
How to write a condition for if when the condition of String in Java ?
you can say
if(z.equalsIgnoreCase("y")){
//code...
}
You are probably looking for equalsIgnoreCase. You can use it like
if (z.equalsIgnoreCase("Y")){//do your job
use equals().
== does reference equality which you don't want.
So use equals() as you want to check the content equality.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
String firstanswer = scan.nextLine();
if(firstanswer == answer2)
{
System.out.println("OK lets get started");
}
else
{
System.out.println("That is incorrect, of course you want to play");
}
//answer2 is set to "yes", i declared it above
make it firstanswer.equals(answer2) instead of firstanswer == answer2.
When you want to check for equality of String in java then use equals method instead of == operator.
equals method checks whether the contents of String objects are same
== operator checks whether both the reference variables refer to same String object
To understand about strings and equality, read String comparison with equals and assignment operator It will help you understand the concept well.
Use equals() instead of == to compare strings.
if(firstanswer.equals(answer2)) is what you're looking for.
firstanswer and answer2 are pointers to string objects. == checks to see whether the pointers are equal (whether they point to the same object), while equals() compares the contents of the two strings and returns a boolean representing whether or not the contents are equal.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I have an if statement that takes a string, and if another string has the same value as that string do 1 thing, and if the variable doesnt equal that string do another thring
here is my code
if(Pos != "D"){
System.out.println("doesnt = D");
}
if (Pos == "D" ){//WHY ISNT THIS WORKING
System.out.println("it does = D");
}
It recognizes when the variable doesnt = D and prints "doesnt = d" but when the variable = D it does nothing. I dont know why.
thanks
Never compare Strings with == or != since these check to see if two String variables refer to the same object reference, and this is not what you're interested in. Instead use the equals(...) or equalsIgnoreCase(...) method to see if the two Strings have the same chars in the same order as that's what really matters here. i.e.,
Use equals to compare strings :
if ("D".equals(Pos))