比较字符串值和整数值?

时间:2020-09-24 07:41:15

标签: spring-boot

如果我想将String值与-1(int)值进行比较该怎么办? 在我的代码“ product.getCode()”中返回字符串值,我必须检查-1值。

2 个答案:

答案 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
}
相关问题