2015-02-06 63 views
0

我想问几个问题,请参考我在代码中添加的注释部分,谢谢。关于Java中的一些简单的异常处理

package test; 

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class Test { 
    /* Task: 
    prompt user to read two integers and display the sum. prompt user to read the number again if the input is incorrect */ 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     boolean accept_a = false; 
     boolean accept_b = false; 
     int a; 
     int b; 

     while (accept_a == false) { 
      try { 
       System.out.print("Input A: "); 
       a = input.nextInt();   /* 1. Let's enter "abc" to trigger the exception handling part first*/ 

       accept_a = true; 
      } catch (InputMismatchException ex) { 
       System.out.println("Input is Wrong"); 
       input.nextLine();     /* 2. I am still not familiar with nextLine() parameter after reading the java manual, would you mind to explain more? All I want to do is "Clear Scanner Buffer" so it wont loop for the println and ask user to input A again, is it a correct way to do it? */ 

      } 
     } 

     while (accept_b == false) { 
      try { 
       System.out.print("Input B: "); 
       b = input.nextInt(); 
       accept_b = true; 
      } catch (InputMismatchException ex) {    /*3. Since this is similar to the above situation, is it possible to reuse the try-catch block to handling b (or even more input like c d e...) exception? */ 

       System.out.println("Input is Wrong"); 
       input.nextLine(); 
      } 

     } 
     System.out.println("The sum is " + (a + b)); /* 4. Why a & b is not found?*/ 

    } 
} 

回答

1
  • 我仍然不熟悉nextLine()参数读数java的手册后,你会介意解释吗?我想要做的只是“清除扫描缓冲区”,因此它不会为println循环并要求用户再次输入A,这是否是正确的方法?
  • input.nextInt();后使用的input.nextLine();是清除从输入流中的其余内容,(至少)的新行字符仍然在缓冲器中,留在缓冲器中的内容将导致input.nextInt();到继续抛出Exception如果它没有被清除第一

  • 由于这是与上述类似的情况下,是有可能再利用try-catch块到处理b(或甚至更多输入如cd e ...)异常?
  • 你可以,但如果输入b错误会发生什么?你是否要求用户重新输入一个?如果你有100个输入,并且他们得到最后一个错误,会发生什么?你最好写一个这样做的方法,那就是提示用户输入一个值并返回该值的方法。

    示例...

    public int promptForIntValue(String prompt) { 
        int value = -1; 
        boolean accepted = false; 
        do { 
         try { 
          System.out.print(prompt); 
          value = input.nextInt(); 
          accepted = true; 
         } catch (InputMismatchException ex) { 
          System.out.println("Input is Wrong"); 
          input.nextLine(); 
         } 
        } while (!accepted); 
        return value; 
    } 
    
  • 为什么一个& b为没有发现?
  • 因为他们还没有被初始化,编译器不能确保他们有一个有效的价值...

    试着改变它更多的东西一样。

    int a = 0; 
    int b = 0; 
    
    +0

    完美答案,尤其是对nextLine();解释,现在我知道它是如何工作的。谢谢, – Bilo 2015-02-06 05:53:54

    +0

    @BiloChan这是一个常见的误解;)。很高兴帮助 – MadProgrammer 2015-02-06 05:55:39

    1
    1. 是的,没关系。并将消耗非整数输入。
    2. 是的。如果我们将它提取到一个方法。
    3. 因为编译器认为它们可能未被初始化。

    让我们简化和提取方法,

    private static int readInt(String name, Scanner input) { 
        while (true) { 
         try { 
          System.out.printf("Input %s: ", name); 
          return input.nextInt(); 
         } catch (InputMismatchException ex) { 
          System.out.printf("Input %s is Wrong%n", input.nextLine()); 
         } 
        } 
    } 
    
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in); 
    
        int a = readInt("A", input); 
        int b = readInt("B", input); 
        System.out.println("The sum is " + (a + b)); 
    } 
    
    0

    我已经把评论这个问题行。

    package test; 
    
    import java.util.InputMismatchException; 
    import java.util.Scanner; 
    
    public class Test { 
    
    public static void main(String[] args) { 
        Scanner input = new Scanner(System.in); 
    
        boolean accept_a = false; 
        boolean accept_b = false; 
        int a=0; 
        int b=0; 
    
        System.out.print("Input A: "); 
    
        while (accept_a == false) { 
         try { 
    
          a = input.nextInt(); // it looks for integer token otherwise exception  
    
          accept_a = true; 
         } catch (InputMismatchException ex) { 
          System.out.println("Input is Wrong"); 
          input.next(); // Move to next other wise exception // you can use hasNextInt()   
    
         } 
        } 
    
        System.out.print("Input B: "); 
        while (accept_b == false) { 
         try { 
    
          b = input.nextInt(); 
          accept_b = true; 
         } catch (InputMismatchException ex) {  
    
          System.out.println("Input is Wrong"); 
          input.next(); 
         } 
    
        } 
        System.out.println("The sum is " + (a + b)); // complier doesn't know wheather they have initialised or not because of try-catch blocks. so explicitly initialised them. 
    
    } 
    

    }