Java 如何使它接受两个命令行参数

Java 如何使它接受两个命令行参数,java,try-catch,java.util.scanner,string-conversion,Java,Try Catch,Java.util.scanner,String Conversion,我似乎把事情搞砸了,因为当我运行代码时,它在我的第一个if语句中失败,输出为“必须有2个cmd行参数” 我正试图找出我把事情搞砸的地方,不幸的是,我的桌面不允许我使用netbeans或eclipse来尝试解决这个问题。基本上,我希望在得到关于X和Y值的提示后,当我输入命令行参数时,它接受这两个命令行参数 这是我的密码: import java.util.*; public class GenerationX { public static void main(String[] args)

我似乎把事情搞砸了,因为当我运行代码时,它在我的第一个if语句中失败,输出为“必须有2个cmd行参数”

我正试图找出我把事情搞砸的地方,不幸的是,我的桌面不允许我使用netbeans或eclipse来尝试解决这个问题。基本上,我希望在得到关于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;
    }
}
import java.util.*;
公共类生成X{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入什么数字?记住您需要一个“X”和一个“Y”输入:”);
int i;
int num=0;
字符串str=“”;
int j=input.nextInt();
int k=input.nextInt();
如果(args.length<2){//如果输入的args少于两个,则退出
System.out.println(“必须有两个命令行参数”);
系统出口(0);
}否则{
随机=新随机();
int repeat=validateArg(args[0]);
int range=validateArg(args[1]);
对于(int count=0;count如果(结果)已编辑

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

Integer[] array = {null, null};
array[0] = j;
array[1] = k;
if (array[0] == null || array[1] == null){
    //insert code to do stuff here
}
然后,if语句应检查其中一个值是否为null,如下所示:

Integer[] array = {null, null};
array[0] = j;
array[1] = k;
if (array[0] == null || array[1] == null){
    //insert code to do stuff here
}
args
实际上是由程序第一次运行时在命令行中输入的内容组成的数组。因此,要向args添加值,用户必须打开命令行并执行


java[ClassName][arg0][arg1]…

所以我在@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;
    }
}
import java.util.*;
公共类随机性{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想输入什么数字?记住您需要一个“X”和一个“Y”输入:”);
int num=0;
字符串str=“”;
int i=input.nextInt();
int j=input.nextInt();
整数[]数组={null,null};
数组[0]=i;
数组[1]=j;
如果(数组[0]==null | |数组[1]==null){//如果输入的参数少于两个,则退出
System.out.println(“必须有2个命令行参数”);
系统出口(0);
}否则{
随机=新随机();
int repeat=validateArg(数组[0]);
int range=validateArg(数组[1]);
对于(int count=0;count如果(arg命令行参数来自于您如何执行应用程序。例如:
java GenerationX arg1 arg2
您如何执行应用程序?不幸的是,我不得不使用在线编译器browxy,无法在工作中使用我的命令行,但在您的情况下,我理解您的意思。我希望能够提示用户输入两个参数,然后让代码运行,我试着按照你的建议去做,创建了一个新的数组,并在我的初始字符串方法之后添加了它,并将我的第一个if语句重写为你的建议,但我得到的错误是找不到符号变量j和k以及不可比类型int和null,所以我假设我接受了你的advice并以不正确的方式实现了它。我发现了问题。实际上我有点不对劲。您希望使用整数对象而不是原始整数。我正在编辑我的答案以反映这一点。此外,您还希望创建数组并将其添加到以下行:
int k=input.nextInt()
好的,由于您的输入,它解决了整个问题,这就是为什么我提交它作为答案,但现在当我运行程序时,它工作了,然后我输入了2个随机值,例如24 15,我得到了一个“线程中的异常”main“java.lang.ArrayIndexOutOfBoundsException:0 at GenerationX.main(GenerationX.java:21)”错误我假设第21行是行
int repeat=validateArg(args[0])
args
更改为
array
如果不是,哪一行是21?这对您的第21行是正确的?我想首先说,您是一个救生员,我整天都在做这件事,没有工具可以帮助,您的评论会产生巨大的影响。我将args更改为array,并得到一组全新的错误。编译错误rs检测到行:20,类GenerationX中的validateArg方法无法应用于给定类型;必需:java.lang.String,找到:java.lang.Integer原因: