我如何使它接受命令行参数

时间:2016-02-23 17:38:57

标签: java try-catch java.util.scanner string-conversion

我似乎弄乱了一些东西,因为当我运行代码时,它在我的第一个if语句中失败,输出为"必须有2 cmd line args"。

我试图找出我搞砸的地方,不幸的是我在桌面上不允许我使用netbeans或eclipse来尝试解决这个问题。基本上我希望它接受两个命令行args,当我输入它们后,我得到我的X和Y值的提示。

这是我的代码:

import java.util.*;
public class GenerationX {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(" What numbers would you like to enter? Remember you need an 'X' and a 'Y' input: ");
        int i;
        int num = 0;
        String str = " ";
        int j = input.nextInt();
        int k = input.nextInt();

        if(args.length < 2) { //exits if less than two args entered
            System.out.println("must have two cmd line args");
            System.exit(0);
        } else {
            Random random = new Random();
            int repeat = validateArg(args[0]);
            int range = validateArg(args[1]);
            for(int count = 0; count < repeat; count++) {
                System.out.printf("%s ", random.nextInt(range));
            //Process each character of the string;
                while( j < str.length() && k < str.length()) {
                    num *= 10;
                    num += str.charAt(j++) - '0'; //Minus the ASCII code of '0' to get the value of the charAt(i++).
                    num += str.charAt(k++) - '0';
                }
                System.out.println();
            }
        }
    }
    static int validateArg(String arg) {
        int result = 0;

        try { //tries to parse first arg into variable
            result = Integer.parseInt(arg);
        }
        catch (NumberFormatException e) {
            System.out.println("arg bad. Prog exiting.");
            System.exit(0);
        }

        if(result <= 0) {
            System.out.println("arg must be positive");
            System.exit(0);
        }
        return result;
    }
}

我不确定我是否需要用于扫描仪输入的int k方法,但是我尝试让代码接受两个args以绕过第一个if语句失败,我也想离开这个in以便为此添加纠正措施。

2 个答案:

答案 0 :(得分:1)

<强>被修改

您需要创建一个新数组并将j和k添加到数组中,如下所示:

Integer[] array = {null, null};
array[0] = j;
array[1] = k;

然后你的if语句应检查这两个值是否为null:

if (array[0] == null || array[1] == null){
    //insert code to do stuff here
}

args实际上是一个数组,它是在程序首次运行时从命令行输入的内容中制作的。因此,要向args添加值,用户必须打开命令行并执行

java [ClassName] [arg0] [arg1] ...

答案 1 :(得分:0)

所以我在@Jonah Haney的帮助下找到了它,并且在Java大师指出我只需要改变一个词之前还有几个小时的盯着它。

继承人的工作代码:

import java.util.*;
public class RandomXY {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(" What numbers would you like to enter? Remember you need an 'X' and a 'Y' input: ");
        int num = 0;
        String str = " ";
        int i = input.nextInt();
        int j = input.nextInt();
        Integer[] array = {null, null};
        array[0] = i;
        array[1] = j;

        if(array[0] == null || array[1] == null) { //exits if less than two args entered
            System.out.println("must have 2 command line arguments");
            System.exit(0);
        } else {
            Random random = new Random();
            int repeat = validateArg(array[0]);
            int range = validateArg(array[1]);
            for(int count = 0; count < repeat; count++) {
                System.out.printf("%s ", random.nextInt(range));
            //Process each character of the string;
                while( i < str.length() && j < str.length()) {
                    num *= 10;
                    num += str.charAt(i++) - '0'; //Minus the ASCII code of '0' to get the value of the charAt(i++).
                    num += str.charAt(j++) - '0';
                }
                System.out.println();
            }
        }
    }
    private static int validateArg(Integer arg) {
        if(arg <= 0) {
            System.out.println("arguments must be positive");
            System.exit(0);
        }
        return arg;
    }
}