2015-09-04 50 views
1

我查看了一堆其他問題和在線,但我找不到任何具體答案。我試圖處理和基於Windows命令行參數確定有效的輸入。作爲一個樣本,我只是看到輸入的數字是否是積極的,儘可能簡單。最大的問題是什麼讓我無法找到具體的答案,我真的試圖使用遞歸來讓它不斷詢問,直到輸入有效的輸入,而不是僅僅殺死程序。在java中,我如何處理有關Windows命令行參數的異常?

Algorithm: 
If there is an argument provided and it's a positive integer 
    Display value 
Otherwise 
    Until the argument is a positive number 
     Prompt for a positive integer 
    Display value 

我搏鬥的代碼,並最終得到了這個工作,但它似乎真的效率低下,重複和砍死在一起。起初,我在被捕獲的異常中有while循環,但是這允許其他事情從命令行中滑落。我怎樣才能使這個儘可能高效,並防止任何邏輯錯誤或異常?在解決這個問題時,我應該採用什麼方法來處理我的算法?這裏是我的代碼:

import java.util.Scanner; 


public class Test 
{ 
    public static void main(String[] args) 
    { 
     String arg; 
     Scanner user_input = new Scanner(System.in); 
     int i = 0; 

     try { 
      arg = args[0]; 
      i = Integer.parseInt(arg); 
     } catch(ArrayIndexOutOfBoundsException e) { 
      arg = ""; 
     } catch(NumberFormatException e2) { 
      arg = ""; 
     } 

     while(i <= 0) 
     { 
      System.out.print("Please type in a positive whole number. "); 
      arg = user_input.next(); 

      try { 
       i = Integer.parseInt(arg); 
      } catch(NumberFormatException e2) { 
       System.out.print("That's a letter! "); 
       continue; 
      } 

      if(i <= 0) 
      { 
       System.out.print("That's a negative. "); 
      } 
     } 


     System.out.println("Input is " + i); 
    } 
} 

回答

1

試試這個:

的代碼是相當長,這是因爲需要兩個單獨的try塊;一個命令行參數&其他爲通過掃描儀提供的參數...

我必須創建自己的自定義異常,「NegativeNumberException」 ......

 import java.util.Scanner; 

    public class NegativeNumberException extends Exception{ 

     NegativeNumberException(){ 
      System.out.println(exceptionMessage); 
     } 

     String exceptionMessage = "Number must be positive"; 
     static int num; 

     public static void main(String[] args) throws NegativeNumberException{ 

      try 
      { 
      if(Integer.parseInt(args[0])<0){ 
       throw new NegativeNumberException(); 
      } 
      else{ 
       int num = Integer.parseInt(args[0]); 
       System.out.println("Your number is: " + num); 

      } 
      } 
      catch(NumberFormatException ex){ 
       System.out.println("That's not even a number."); 

      } 
      catch(NegativeNumberException ex){ 
       ex.getMessage(); 
      } 



      while(num==0){ 
      try{ 
       System.out.println("Enter a positive number:"); 
       Scanner input = new Scanner(System.in); 
       int num1 = input.nextInt(); 
       if(num1<0){ 
        throw new NegativeNumberException(); 
       } 
       num = num1; 
       break; 
      }catch(Exception ex){ 
       System.out.println("Positive number only, try again..."); 
       } 
      }//End While 

      System.out.println("Your number is:" + num); 
      } 

    } 

輸入:(命令行):笑

輸出

 (Console):That's not even a number 
        Enter a positive int 

     (Console input via Scanner): -4 

     (Console):Number must be positive 
       Positive number only, try again... 
       Enter a positive number: 

     (Console input via Scanner): 3 


     (Console):Your number is: 3 
+0

謝謝!創建一個全新的異常有點超出了我目前的經驗,所以我沒有想到它,但這絕對是以最簡潔,最優雅和最有效的方式回答我的問題,並在編譯器級別構建安全網。對此,我真的非常感激!現在我只需要學習如何做出異常:) – BrainFRZ

+0

@TerryWeiss你的歡迎,請upvote,如果我幫助... – RamanSB

+0

對不起,我試過了,但我還沒有足夠的聲望呢。你當然有幫助,而且我確實選擇你的答案作爲接受的答案,如果這能幫助你。 – BrainFRZ

0

我修改了你的代碼,使它以你想要的方式運行。試試這個:

public static void main(String[] args) { 
    String arg; 
    Scanner user_input = new Scanner(System.in); 
    int numTries = 0, value = 0; 

    try { 
     numTries = Integer.parseInt(args[0]);  // get max number of tries 
    } catch (ArrayIndexOutOfBoundsException e) { 
     arg = ""; 
    } catch (NumberFormatException e2) { 
     arg = ""; 
    } 

    // try 'numTries' times to read a valid number input 
    for (int i=numTries; i > 0; --i) { 
     System.out.print("Please type in a positive whole number: "); 
     arg = user_input.next(); 

     try { 
      value = Integer.parseInt(arg); 
     } catch(NumberFormatException e2) { 
      System.out.println("That's a letter! "); 
      continue; 
     } 

     if (value <= 0) { 
      System.out.println("That's a negative. "); 
     } 
     break;          // exit loop if number input found 
    } 

    System.out.println("Input is " + value); 
} 

這段代碼已經在IntelliJ上測試過了,沒有問題。