【发布时间】:2013-02-23 10:08:26
【问题描述】:
我还是一个 Java 新手,我有这个代码。我不知道如何将输入文件传递给代码。我正在使用 Eclipse Juno。
public static void main(String[] args) {
In in = new In(args[0]); // input file
int N = in.readInt(); // N-by-N percolation system
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(N);
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
每当我运行它时,我都会得到这个异常:
线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationVisualizer.main(PercolationVisualizer.java:42)
什么可能导致此异常?能否请您耐心解释一下代码中如何调用输入文件的过程?
【问题讨论】:
-
42是哪一行?如果它是第一个什么(我猜的),那么你会得到异常,因为你没有将任何命令行参数传递给你的应用程序。 -
使用 Eclipse 运行程序时,您可以在启动配置的
Arguments选项卡中指定命令行参数。见stackoverflow.com/questions/4065920/… 和tutorial。