readLine读取,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();
        }
    }
}

 

Enter n1:a
Illegal integer format
Enter n1:1
Enter n2:-1
The total is 0

posted on 2009-11-18 23:41  我为Java而努力  阅读(910)  评论(0)    收藏  举报

导航