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

时间:2015-09-04 02:51:37

标签: java exception command-line user-input

我在网上查看了一堆其他问题,但我无法找到任何具体的答案。我试图根据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);
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

代码非常冗长,这是因为需要两个独立的try块;一个用于命令行参数&amp;另一个是通过扫描仪提供的参数...

我必须创建自己的自定义异常,“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);
            }

    }

输入:(命令行):lol

<强>输出

       (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

答案 1 :(得分: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上进行了测试,运行时没有任何问题。