0

i am new to java and i've downloaded a pdf document from wikibooks, and while trying to run a program i am getting following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Distance.main(Distance.java:7)

This is the code i am trying to run:

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);
    }
}
2
  • What's your input and output? Commented Dec 14, 2012 at 15:08
  • the program is not even compiling it just shows an error and that's it..nothing else.. Commented Dec 14, 2012 at 15:12

1 Answer 1

6

You did not provide parameters to command-line java invocation or in your IDE configuration.

It seems it needs four integer parameters, as in:

java Distance 0 1 2 3
Sign up to request clarification or add additional context in comments.

10 Comments

Run- > Run Configurations-> (Find your configuration - most likely Java Application called Distance) -> Arguments Tab -> Program arguments -> Run.
can you tell me what is this and why is it used, please if you have time?
Well, it seems your program calculates the Euclidean distance between two points on a 2D plane. It could either generate those points randomly, or it can use the user's input. In this case, it is the latter.
then do i have to go all this process while trying to run this "everytime"?
Once you add it to the configuration once, it is saved, and it will use the same input until you change it.
|

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.