我还是一个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);
}
}
每当我运行它时,我都会收到此异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationVisualizer.main(PercolationVisualizer.java:42)
什么可能导致此异常?请您耐心解释一下代码中如何调用输入文件的过程?
最佳答案
引用this guide用于向您的程序添加参数。或者,您可以直接在代码中指定文件名,而不是从参数中读取它。
基本上,该指南指示用户转到“运行”菜单,然后“运行...”(在最近的 Eclipse 版本中实际上是“运行配置...”),为所需项目选择适当的运行配置,单击“参数”选项卡,然后在“程序参数”部分中输入参数(例如文件名),并用空格分隔。
关于java - 如何在这段代码中输入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15039319/