【发布时间】:2014-04-21 04:39:02
【问题描述】:
我需要输入一个字符串输入,格式化它,并以某种格式显示输出。我应该使用 Run > Run Configuration > Arguments > Program Argument 并在 Program arguments 部分输入任何字符串(即“test”)。当我尝试运行 java 应用程序时,我不断在控制台窗口中收到一条错误消息。如何使用命令行参数?我应该在程序参数中输入什么?对此的任何帮助,将不胜感激。谢谢。
以下是我在控制台窗口中收到的错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at a00752124.data.InventoryReader.read(InventoryReader.java:26)
at a00752124.Lab2.<init>(Lab2.java:38)
at a00752124.Lab2.main(Lab2.java:28)
下面是我的主类源代码:
public class Lab2 {
/**
* Main method of Lab2 class
*
* @param args
*/
public static void main(String[] args) {
new Lab2(args[0]);
}
/**
* Constructor for Lab2 class
*
* @param itemCount
*/
public Lab2(String itemCount) {
Item[] items = InventoryReader.read(itemCount);
System.out.println(Arrays.toString(items));
InventoryReport.display(items);
}
}
这是我的 InventoryReader 读取方法的源代码:
public static Item[] read(String input) {
String[] rows = input.split(":");
Item[] items = new Item[rows.length];
int i = 0;
for (String row : rows) {
String[] element = row.split("\\|");
items[i] = new Item(element[0], element[1], Integer.valueOf(element[2]), Float.valueOf(element[3]));
i++;
}
return items;
}
【问题讨论】:
-
异常发生在
InventoryReader.read()方法中。你能告诉我们那个方法吗?确切位置是InventoryReader.java第 26 行。 -
您好,感谢您的意见。我在上面添加了我的 InventoryReader 读取方法的源代码。
-
看来你需要输入
:作为行分隔符和|作为列分隔符,行中有4个元素 -
谢谢我试过了,但它仍然给出错误。我仍然从“items[i] = new Item(element[0], element[1], Integer.valueOf(element[2]), Float.valueOf(element[3])) 行中抛出异常;"
标签: java eclipse command-line-arguments