2015-02-06 65 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. 
    
    } 
    

    }