例1:
String str = "123";try {
int a = Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
}
例2:
try {
int b = Integer.valueOf(str).intValue()
} catch (NumberFormatException e) {
e.printStackTrace();
}
本文提供了两个Java示例,展示了如何将字符串转换为整数,并通过捕获NumberFormatException来处理可能发生的异常。第一个示例使用了Integer.parseInt方法,第二个示例则通过Integer.valueOf方法进行转换。
例1:
String str = "123";例2:

被折叠的 条评论
为什么被折叠?