利用Integer方法将String类型转换为int类型

//A.java

import java.io.*;

public class A {

static BufferedReader br;

public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
try {
int a = exec1();
int b = exec2();
System.out.println("The total is " + (a + b));
} catch (Exception e) {
e.printStackTrace();
}
}

public static int exec1() {
System.out.print("Enter n1:");
try {
return Integer.valueOf(br.readLine());
} catch (Exception e) {
System.out.println("Illegal integer format");
return exec1();
}
}

public static int exec2() {
System.out.print("Enter n2:");
try {
return Integer.valueOf(br.readLine());
} catch (Exception e) {
System.out.println("Illegal integer format");
return exec2();
}
}
}

原文地址:https://www.cnblogs.com/fgm119/p/Integer.html