Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何处理有关Windows命令行参数的异常?_Java_Exception_Command Line_User Input - Fatal编程技术网

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

在java中,如何处理有关Windows命令行参数的异常?,java,exception,command-line,user-input,Java,Exception,Command Line,User Input,我在网上浏览了很多其他的问题,但我找不到任何具体的答案。我正在尝试处理基于Windows命令行参数的有效输入并确定其有效性。作为一个例子,我只是想看看输入的数字是否为正,以便尽可能简单。最大的问题,也是让我无法找到具体答案的原因,是我真的试图使用递归让它一直询问,直到输入有效的输入,而不仅仅是终止程序 Algorithm: If there is an argument provided and it's a positive integer Display value Otherwis

我在网上浏览了很多其他的问题,但我找不到任何具体的答案。我正在尝试处理基于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);
    }
}
import java.util.Scanner;
公开课考试
{
公共静态void main(字符串[]args)
{
字符串arg;
扫描仪用户输入=新扫描仪(System.in);
int i=0;
试一试{
arg=args[0];
i=整数.parseInt(arg);
}捕获(阵列索引边界外异常e){
arg=“”;
}捕获(NumberFormatException e2){
arg=“”;
}

而(i我修改了您的代码,使其按照您想要的方式运行。请尝试使用以下方法:

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);
}
publicstaticvoidmain(字符串[]args){
字符串arg;
扫描仪用户输入=新扫描仪(System.in);
int numTries=0,value=0;
试一试{
numTries=Integer.parseInt(args[0]);//获取最大尝试次数
}捕获(阵列索引边界外异常e){
arg=“”;
}捕获(NumberFormatException e2){
arg=“”;
}
//尝试“numTries”次以读取有效的数字输入
对于(int i=numTries;i>0;--i){
System.out.print(“请键入正整数:”);
arg=user_input.next();
试一试{
值=整数.parseInt(arg);
}捕获(NumberFormatException e2){
System.out.println(“那是一封信!”);
继续;
}
如果(值请尝试以下操作:

代码相当长,这是因为需要两个独立的try块;一个用于命令行参数,另一个用于通过scanner提供的参数

我必须创建自己的自定义异常,“NegativeEnumberException”


谢谢!创建一个全新的异常超出了我目前的经验,所以我没有想到它,但这肯定以最简洁、优雅和高效的方式解决了我的问题,并且在编译器级别构建了一个安全网。我真的很感激它!现在我只需要学习如何做好异常:)很抱歉,我试过了,但我的名声还不够好。不过你确实很有帮助,如果这对你有任何帮助的话,我会选择你的答案作为公认的答案。
        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