码迷,mamicode.com
首页 > 编程语言 > 详细

Java 如何将String转化为Int

时间:2019-12-04 20:19:42      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:format   需要   方法   转化   str   字符   except   异常   ack   

在 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();

}

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

Java 如何将String转化为Int

标签:format   需要   方法   转化   str   字符   except   异常   ack   

原文地址:https://www.cnblogs.com/zhuyeshen/p/11984780.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!