问题描述
我目前正在进行一项任务,但在运行我的代码时似乎存在问题。
I am currently working on an assignment but there seems to be a problem when running my code.
public class caesar {
public static void main(String args[]) {
String inputString = args[0];
char inputArray[] = inputString.toCharArray();
int shiftLength = Integer.parseInt(args[1]);
System.out.println("Input: " + inputString);
String outputString = "";
这是我收到的错误:
线程main中的异常java.lang.ArrayIndexOutOfBoundsException:0 at caesar.main(caesar.java:3)
推荐答案
您没有将命令行参数传递给您的程序,也不检查它们是否被传递。要传递参数,请启动您的程序,如
You are not passing command line arguments to your program and don't check whether they are passed. To pass arguments launch your program like
java caesar arg0 arg1
例如:
java caesar somestring 10
要在NetBeans 8.0.2 IDE中执行此操作,请打开项目属性,选择运行项,然后在其中指定参数:
To do this in NetBeans 8.0.2 IDE, open Project Properties, select the Run item, then specify the arguments there:
您可能还想查看号码提前传递参数以输出友好错误消息。例如:
You may probably also want to check the number of passed arguments in advance to output the friendly error message. For example:
public static void main(String args[]) {
if(args.length != 2) {
System.err.println("Usage: java caesar <inputString> <shift>");
return;
}
... // the rest of your code
}
这篇关于启动java程序时出现ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!