What to do if I want to compare String value to -1(int) value? In my code "product.getCode()" returns string value and I have to check with -1 value.
Asked
Active
Viewed 128 times
-2
-
Does this answer your question? [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – Mario Codes Sep 25 '20 at 11:44
2 Answers
0
You can convert String
to int
value.
String code = product.getCode();
intn codeValue;
try {
codeValue = Integer.parseInt(code);
} catch (NumberFormatException e) {
codeValue = 0;
}
System.out.println(codeValue);
System.out.println(codeValue == -1);

Derek Wang
- 10,098
- 4
- 18
- 39
0
Perform type conversion of String value to integer using Integer.parseInt()
and then continue to compare like below:
int code = Integer.parseInt(product.getCode());
if(code == yourValue)
{
//business logic
}

Ayush28
- 359
- 3
- 18