Why Doesn't My Compare Work Between Char and Int in Java

Compare int to character constant in Java

The most efficient way would be to work with integers - something like final int V0005 = 0x30303035 (a combination of a couple of different ideas seen here) would be manageable. Java just doesn't have the kind of integer literals you are looking for.

If you really want to work with strings, you need to read the characters in a byte array first, then convert that to a String (the default charset normally works as it is a superset of ASCII). However, it would be less efficient.

You could write a function to calculate the constant values from the simple numbers (like 5) for better readability, but that's probably overkill.

How can I convert a char to int in Java?

The ASCII table is arranged so that the value of the character '9' is nine greater than the value of '0'; the value of the character '8' is eight greater than the value of '0'; and so on.

So you can get the int value of a decimal digit char by subtracting '0'.

char x = '9';
int y = x - '0'; // gives the int value 9

How to compare char with number in Java

You can use a lookup "table", I used a String:

private static final String LOOKUP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

And then compare the chars with indexOf(), but it seems messy and could probably be achieved more easily, I just can't come up with something easier at the moment:

String FindCountry = "9Z";

Map<String, String> Cont = new HashMap<>();

Cont.put("BA-BE", "Angola");
Cont.put("9X-92", "Trinidad & Tobago");

for (String key : Cont.keySet()) {
if (LOOKUP.indexOf(key.charAt(0)) == LOOKUP.indexOf(FindCountry.charAt(0)) &&
LOOKUP.indexOf(FindCountry.charAt(1)) >= LOOKUP.indexOf(key.charAt(1)) &&
LOOKUP.indexOf(FindCountry.charAt(1)) <= LOOKUP.indexOf(key.charAt(4))) {
System.out.println("Country: " + Cont.get(key));
}
}

Why doesn't my string comparison work?

Your second comparison is wrong. You should also use equals instead of ==, like this:

if (action.trim().equals("something"))

The == operator compares references of (String) objects and under normal circumstances equal strings don't automatically have the same reference, i.e. they are different objects. (Unless both are internalized, but normally you shouldn't consider it)

Other than that your example works fine and the first comparison is valid. Try fixing the second comparison. If it works, you found your problem. If not, try using a debugger and double-check everything.

PS: When comparing literal strings with dynamic string objects, it's good practice to call the equals method on the literal string:

"something".equals(action)

That way you can avoid NullPointerExceptions when the string object is null.

Java string comparison not working as intended

It is because you are not comparing 2 Strings. You have to put it like this:

Log.e("testLogic", String.valueOf(taken.getText().toString().trim().equals("1")));

because .equals() function needs two Strings. Supposing that s1 and s2 are Strings, you should do:

s1.equals(s2);

I expect it will be helpful for you!



Related Topics



Leave a reply



Submit