如果我想将String值与-1(int)值进行比较该怎么办? 在我的代码“ product.getCode()”中返回字符串值,我必须检查-1值。
答案 0 :(得分:0)
您可以将String
转换为int
的值。
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);
答案 1 :(得分:0)
使用Integer.parseInt()
将String值类型转换为整数,然后继续进行如下比较:
int code = Integer.parseInt(product.getCode());
if(code == yourValue)
{
//business logic
}