java, two integers, they are equal but [duplicate] - java

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.

Related

Java If Else Text Based on Other Text [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
I am trying to have a text-box populate with X if another text-box has a certain value, else Y. However, it's populating with X or Y seemingly randomly. d4 is my button, d4result is where it populates the result, d4txt1 is where I want to see a 1 or 0, depending.
d4.setOnClickListener {
if (d4result.text.toString() == "1") {
d4txt1.text = "1"
} else {
d4txt1.text = "0"
}
val rand = Random().nextInt(4) + 1
d4result.text = rand.toString()
}
So if d4result is populated with 1, I want d4txt1 to populate with 1, otherwise it should be zero. But when I try it, I get 1 or 0 and I can't notice a pattern as to when/why.
Use equals instead of ==. == operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
d4.setOnClickListener {
if (d4result.text.toString().equalsIgnoreCase("1")) {
d4txt1.text = "1"
} else {
d4txt1.text = "0"
}
val rand = Random().nextInt(4) + 1
d4result.text = rand.toString()
}
Java is tricky about that. The == operator compares the two object pointers, not their values. It's fine for integers and floats but almost never useful for strings.
Instead use the .equals() method or the .equalsIgnoreCase() method:
if (d4result.text.toString().equalsIgnoreCase("1")) { ...

Return is always true, even when it's false [duplicate]

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;
}

String-Comparison in java does not match [duplicate]

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.

JLabel not comparing to string [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I am going to check if a JLabel is the same as a name of a tv show.
I have the code
public int lastEp() {
if(name.getText() == "Dexter") {
switch (season) {
case 1:
return 12;
case 2:
return 12;
// etc.
}
}
return -1;
}
I have checked in console what i get from name.getText() and the console prints "Dexter".
Still my if statement wont return true.
System.out.println(name.getText() == "Dexter") gives false, while System.out.println(name.getText() + " " + "Dexter") gives "Dexter Dexter".
What is happening?
Also bonus question, if anyone know what i should return instead of -1 if no other option fits, if there is a good standard to follow.
This is an extended comment and the question should be closed
Strings in Java are compared with String#equals not ==
For example,
"Dexter".equals(name.getText())
You are currently comparing the object (memory) reference, which will vary rarely be equal
Check out the Strings trail for more details

If statement wont recognize string [duplicate]

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

Categories