0

I am trying to get the user input using the following code but on running it gives the error that Arrayindexoutofbounds. Don't know what is the problem.

public static void main(String[] args) {
    int T = Integer.parseInt(args[1]);
    PercolationStats stats = new PercolationStats(
            Integer.parseInt(args[0]), T);
    double mean = stats.mean();
    StdOut.println("mean = " + mean);
    double stddev = stats.stddev();
    StdOut.println("stddev = " + stddev);
    double d = (1.96 * stddev) / Math.sqrt(T);
    StdOut.println("95% confidence interval = " + (mean - d) + ", "
            + (mean + d));
}
user3154554
  • 55
  • 1
  • 9
  • What line is throwing the ArrayIndexOutOfBounds complaints? What's does the Eclipse debugger tell you is in args? (I'm betting that you failed to set up your launch configuration to pass and/or prompt for command-line arguments, and args is empty.) – keshlam Feb 02 '14 at 04:22

2 Answers2

1

To pass arguments to a Java program via Eclipse, you should use the Arguments tab in Run > Run Configurations. Example:

Arguments

enter image description here

Code

public static void main(String[] args) {
    System.out.println(args[0]);
    System.out.println(args[1]);
    System.out.println(args[2]);
}

Result

33
twitter
400.23
Jops
  • 22,535
  • 13
  • 46
  • 63
  • Then if i want have a continous input like while(args[0]!=EOF){ add to array } How can we implement this. – user3154554 Feb 02 '14 at 05:39
  • If that's the case, you're better off passing a filename into the first argument. You can then parse that file during processing. See this link: http://stackoverflow.com/questions/1055318/using-command-line-argument-for-passing-files-to-a-program – Jops Feb 02 '14 at 05:55
0

you can pass your input parameters in following way in Eclipse: click menu Run -> Run Configurations -> Arguments in Program arguments put your arguments, which will be loaded to "String [] args" you could just separate your arguments by space:

e.g.

1 2 3

and your args = {1,2,3}

tanghao
  • 4,073
  • 2
  • 13
  • 17
  • Then if i want have a continous input like while(args[0]!=EOF){ add to array } How can we implement this. – user3154554 Feb 02 '14 at 05:43
  • you can use java bufferedreader to read your input from a file. You can refer the example in this link: [How to read file in Java – BufferedReader](http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/) – tanghao Feb 02 '14 at 06:54