zhuyeshen

在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换.

例1:


String str = "123";

try {

    int a = Integer.parseInt(str);

} catch (NumberFormatException e) {

    e.printStackTrace();

}

例2:


String str = "123";

try {

    int b = Integer.valueOf(str).intValue()

} catch (NumberFormatException e) {

    e.printStackTrace();

}

在转换过程中需要注意,因为字符串中可能会出现非数字的情况,所以在转换的时候需要捕捉处理异常

分类:

技术点:

相关文章:

  • 2021-09-13
  • 2021-09-13
  • 2021-12-24
  • 2021-12-24
  • 2021-11-15
  • 2021-08-06
  • 2021-11-05
  • 2021-10-07
猜你喜欢
  • 2021-12-24
  • 2021-09-13
  • 2021-12-24
  • 2021-12-24
  • 2021-11-06
  • 2021-10-11
相关资源
相似解决方案