0

I am still a Java newbie and I have this code. I don't know how to pass the input file to the code. I am using 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);
    }
}

Whenever I run it I get this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at PercolationVisualizer.main(PercolationVisualizer.java:42)

What might cause this exception? Could you please be patient with me and explain the process of how to call the input file in the code?

2
  • Which one is line 42? If it's the first what (what I guess), then you get the exception because you did not pass any command line parameter to your application. Commented Feb 23, 2013 at 10:14
  • When running programs with Eclipse you can specify the command line arguments in the Arguments tab of the launch configuration. See stackoverflow.com/questions/4065920/… and tutorial. Commented Feb 23, 2013 at 10:16

2 Answers 2

2

Refer to this guide for adding arguments to your program. Alternatively, you could specify the file name directly in the code instead of reading it from the args.

Basically, the guide instructs the user to go to the Run menu, then "Run..." (actually "Run Configurations..." in recent Eclipse versions), select the appropriate run configuration for the desired project, click the Arguments tab, and enter the arguments (such as file name) in the "Program arguments" section, separated by spaces.

Sign up to request clarification or add additional context in comments.

Comments

0

For those using IntelliJ you can set the program arguments via Run->Edit Configuration. Look down about the middle of the window to locate the "Program Arguments" field. Then add the path to the test file and save.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.