-4

When I run this code, it comes up with the Exception message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 thefirstone.Distance.main(Distance.java:18)

I'm copying this from a tutorial and it says if you receive the error message I received, it's because "you didn't provide enough numbers." Not sure what they're talking about.

The code:

  public class Distance {
 private java.awt.Point point0, point1;

 public Distance(int x0, int y0, int x1, int y1) {
   point0 = new java.awt.Point(x0, y0);
   point1 = new java.awt.Point(x1, y1);
 }

 public void printDistance() {
  System.out.println("Distance between " + point0 + " and " + point1
                  + " is " + point0.distance(point1));
}

public static void main(String[] args) {
  Distance dist = new Distance(
           intValue(args[0]), intValue(args[1]),
           intValue(args[2]), intValue(args[3]));
  dist.printDistance();
}

private static int intValue(String data) {
  return Integer.parseInt(data);
}
}
4
  • You can find the answer just typing your question in google. This is a duplicate question as well. Next time, search before you ask please. Commented Mar 29, 2016 at 17:56
  • you need to set up your run configuration. What IDE are you using, or are you just using the command line? Commented Mar 29, 2016 at 18:00
  • @flkes I'm using Eclipse Commented Mar 31, 2016 at 17:55
  • To populate String[] args you need to add in program arguments. Follow this stackoverflow.com/a/19648592/3280538 answer. For your program type in 4 numbers separated by spaces Commented Mar 31, 2016 at 18:02

1 Answer 1

0

Exception ArrayOutOfBoundException can only came due to this line of code

 Distance dist = new Distance(
           intValue(args[0]), intValue(args[1]),
           intValue(args[2]), intValue(args[3]));

Please Verify the number of argument passed at the time of execution. if it is less than 4 in numbers then only this Exception might you got.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.