1
InputStreamReader inp= new InputStreamReader(System.in);
do  
{  
  BufferedReader br = new BufferedReader(inp);  
  System.out.println("Enter Roll number : ");  
  int rno= Integer.parseInt(br.readLine());   

  System.out.println("Enter name: ");  
  String name= br.readLine();

  System.out.println("Continue y/n: ");  
  char ans=(char)br.read();  
} while(ans=='y');    

我怎样才能BufferedReader在循环之外写?如果我在外面使用它,它会显示以下异常:

java.lang.numberformatexception for input string at            
    java.lang.Integer.parseInt(Integer.java:468)

是否有任何机制来刷新BufferedReader?可以做什么??

4

1 回答 1

0

这不是“好”,但对我有用 - 我添加了 br.readLine():

public static void main(String args[]) throws IOException {
    BufferedReader br = null;
    try {
        InputStreamReader inp = new InputStreamReader(System.in);
        br = new BufferedReader(inp);
        char ans;
        do {
            System.out.println("Enter Roll number : ");
            int rno = Integer.parseInt(br.readLine());

            System.out.println("Enter name: ");
            String name = br.readLine();

            System.out.println("Continue y/n: ");
            ans = (char) br.read();
            br.readLine();
        } while (ans == 'y');
    } finally {
        if (br != null) {
            br.close();
        }
    }
}
于 2012-06-03T07:24:59.283 回答