我是第一个Java类的新程序员。这是我的代码,但我收到了一个错误
线程"main“java.lang.ArrayIndexOutOfBoundsException:0 at CmdLine.main(CmdLine.java:13)中的异常
import java.util.Scanner;
import java.util.Arrays;
public class CmdLine{
public static void main( String [] args ){
int[] array;
array = new int [ 10 ];
for(int counter = 0; counter < array.length;counter++ )
array[counter]=Integer.parseInt (args[counter]);
System.out.printf( "%s%8s\n", "Index", "Value" );
for(int counter = 0; counter < array.length; counter++ )
System.out.printf( "%5d%8d\n", counter, array[ counter ] );
}
}发布于 2018-02-01 07:09:21
解决这一问题的方法有很多。
-方法:
我知道大多数IDE允许您添加命令行参数。如果您对此感兴趣,请尝试查看IntelliJ和此链接以获得进一步说明:
How do you input commandline argument in IntelliJ IDEA?
还有一种方法是使用jar文件,
Passing on command line arguments to runnable JAR
基本上,Jar文件在这样的终端命令行上工作
java -jar (type_Something)
您可以编写文件路径或单词。
-解释:
当传入命令行参数时,首先传入的是args。
例如。
java -jar你好
Hello会存储在args中,然后您可以这样使用它:
public class Test{
public static void main(String[] args){
String argument1 = args[0];
}
}但是如果你现在添加
java -jar你好朋友
Hello存储在args中,朋友存储在args1中。因此,您可以这样使用它:
public class Test{
public static void main(String[] args){
String argument1 = args[0];
String argument2 = args[1];
}
}-个人经历:
我以前用过第二种方法,用一个罐子。我相信在我的IDE (IntelliJ)终端上使用它之前,我必须在IDE中添加一个Jar。我必须向我的程序发送文件夹路径,唯一允许的方法是使用jar发送命令行参数。
https://stackoverflow.com/questions/48556611
复制相似问题