-4

I'm really new to coding and is stuck on some java code. I've researched this problem, but can't seem to find the answer, or understand what's wrong with my code.

I'm making a program that takes three integer command-line arguments and prints equal if all three are equal, and not equal otherwise.

My code looks like this

public class ThreeInteger {

    public static void main (String[] args){
          int a = Integer.parseInt(args[0]);
          int b = Integer.parseInt(args[1]);
          int c = Integer.parseInt(args[2]);

          if ((a==b) && (b==c) && (a==c)) {
          System.out.println("equal");
          } 

          else {
              System.out.println("not equal");
          } 
    }

}

When i try to run i get get this: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ThreeInteger.main(ThreeInteger.java:5)

How do I get rid of this?

3
  • Are you passing three arguments to your program when you run it? Commented Sep 10, 2017 at 12:23
  • 3
    "I'm making a program that takes three integer command-line arguments and" - then I don't pass any ... for whatever reasons. Commented Sep 10, 2017 at 12:24
  • I see. Cheers for the quick response! Commented Sep 10, 2017 at 12:26

1 Answer 1

-3

You must 3 parameter in main while compilation .. because you are trying to access the 3rd element which is not present in array of main.

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.