java中string转换为int的方式

Integer.parseInt(string s)方法

使用java Integer类的parseInt()方法进行转换。由于待转换的字符串中可能出现非数字的情况,需要捕获NumberFormatException的异常。

示例:

public class test1 {
    public static void main(String[] args) {
        String test_str = "123";
        try {
            int test_int = Integer.parseInt(test_str);
            System.out.println(test_int);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/kevinjin2011/article/details/129265511
今日推荐