我仍然是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)
什么可能导致此异常?你能否请耐心等待我解释如何在代码中调用输入文件的过程?
答案 0 :(得分:2)
有关为程序添加参数的信息,请参阅this guide。或者,您可以直接在代码中指定文件名,而不是从args中读取它。
基本上,指南指示用户转到“运行”菜单,然后“运行...”(实际上是“运行配置...”在最近的Eclipse版本中),为所需项目选择适当的运行配置,单击参数选项卡,并在“程序参数”部分中输入参数(例如文件名),用空格分隔。
答案 1 :(得分:0)
对于使用IntelliJ的用户,您可以通过运行->编辑配置来设置程序参数。在窗口中间向下看,以找到“程序参数”字段。然后将路径添加到测试文件并保存。