在java中,我如何处理有关Windows命令行参数的异常?

问题描述:

我查看了一堆其他问题和在线,但我找不到任何具体答案。我试图处理和基于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); 
    } 
} 

试试这个:

的代码是相当长,这是因为需要两个单独的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

我修改了你的代码,使它以你想要的方式运行。试试这个:

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上测试过了,没有问题。