在 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();
}
例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类型。主要通过两种方式实现:使用Integer类中的parseInt()方法和valueOf()方法。这两种方法都需要捕获可能发生的NumberFormatException异常。
16万+

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



