Java If Else Text Based on Other Text [duplicate] - java

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")) { ...

Related

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

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

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.

Converted charAt() doesnt equals to the same String [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
My problem is hidden in next code:
public class saturo {
public String primer, d;
public void start() {
primer = "545640";
//d = "0";
d = String.valueOf(primer.charAt(((primer.length())-1)));
if(d == "0"){
System.out.println("This is equal: d == " + d);
}else{
System.out.println("This is not equal: d == " + d);
}
}
public static void main(String args[]) {
new saturo().start();
}
}
So as you see, the problem is that if i declare String d as "0" than the program will execute it as d is equal to "0" and return true;
But if i get character "0" from a String, convert it to String, and check if this equals to "0" then i have got a false.
I have checked if there is something wrong with character encode, but not, its right in any case. No type mismatches.
Where in this the logic?
If you want to compare the value of 2 strings, you need to use .equals()
So you would do:
if(d.equals("0"){
// your code here
}
Using == compares the strings by reference (same spot in memory) where .equals() compares the value of the strings.
use .equals not == you are comparing by reference not value

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

How to compare strings that were created by substring? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 10 years ago.
I'm wondering why it doesn't work, and why I cant't use the CHAR data type for variables a and b. The point is, how to compare the first and the last digits of number (pr) accordingly such way.
String a, b;
for (int i=100; i<1000;i++) {
for (int j=100; j<1000;j++) {
pr=j*i;
a = String.valueOf(pr).substring(0, 1);
b= String.valueOf(pr).substring(4, 5);
if ((a==b) ) {
System.out.println(pr);
}
}
}
use equals functions
a.equals(b)
If you want to ignore case then use
a.equalsIgnoreCase(b)
This is not JavaScript to use == for String comparison.
Go for equals method
In Java, operator == compares object identities, so if there are two objects of type String with the same content, == will return false for them:
String x = new String ("foo");
String y = new String ("bar");
if (x == y)
System.out.println ("Equal");
else
System.out.println ("Now equal"); // This will be printed
In order to compare String object by content, not by identity, you need to use equals() method like this:
if (x.equals (y))
System.out.println ("Equal"); // This will be printed
else
System.out.println ("Now equal");
Note, that if x is null, then x.equals (y) will throw NullPointerException while x == y will return false if y is not null and true if y is null. To prevent NullPointerException you need to do something like this:
if (x == null && y == null || x != null && x.equals (y))
System.out.println ("Equal"); // This will be printed
else
System.out.println ("Now equal");

Categories