0

Im quite a novice at Java but im trying to run this simple code. Can someone explained me what i should do to make this code work?

public class BinaryGCD {

public static int gcd(int p, int q) {
    if (q == 0) return p;
    if (p == 0) return q;

    // p and q even
    if ((p & 1) == 0 && (q & 1) == 0) return gcd(p >> 1, q >> 1) << 1;

    // p is even, q is odd
    else if ((p & 1) == 0) return gcd(p >> 1, q);

    // p is odd, q is even
    else if ((q & 1) == 0) return gcd(p, q >> 1);

    // p and q odd, p >= q
    else if (p >= q) return gcd((p-q) >> 1, q);

    // p and q odd, p < q
    else return gcd(p, (q-p) >> 1);
}

public static void main(String[] args) {
    int p = Integer.parseInt(args[0]);
    int q = Integer.parseInt(args[1]);
    System.out.println("gcd(" + p + ", " + q + ") = " + gcd(p, q));
}

}

In Eclipse i get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at BinaryGCD.main(BinaryGCD.java:25)

  • 1
    Duplicate of http://stackoverflow.com/questions/373328/invoking-java-main-method-with-parameters-from-eclipse http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse – Shashi Jan 17 '13 at 10:16

6 Answers6

0

Can someone explained me what i should do to make this code work?

Yes, you have to pass 2 parameters to your program. You are not passing any parameters, so args has the length 0, and that is why you cant access the first and second value (index 0 and 1).

jlordo
  • 37,490
  • 6
  • 58
  • 83
0

you most possibly are not passing command line arguments,

 int p = Integer.parseInt(args[0]);
    int q = Integer.parseInt(args[1]);

If you are using eclipse IDE check, How to pass command line args

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

args[0] is first argument of command line. You probably didn't pass any argument so it is a problem. In this program you should have 2 arguments.

In eclipse you should go to Run configuration and add arguments in Program arguments textarea.

partlov
  • 13,789
  • 6
  • 63
  • 82
0

You are not invoking the program with command line arguments. Use java BinaryGCD 20 30 You can replace 20 30 in above example with any other number

  • Thank you, this worked for me. But in Eclipse i just had to put 20 and 30 there. one more question, is it possible to get rid of these args? how can i replace them? – user1873548 Jan 17 '13 at 10:30
  • To get rid of these arguments, you can either assign values to variables in the code or accept the input in some different way. Let us first see the code:int p = 20; int q = 30; Apart from that, you can use some java swing dialog to accept input or text box in web application or Scanner class from accepting user input from command line. Just search for the keywords swing dialog, HTML text box and Scanner class to get started – Piyush-Ask Any Difference Jan 17 '13 at 11:12
0
int p = Integer.parseInt(args[0]);
int q = Integer.parseInt(args[1]);

you need two arguments for your program.

1st arg: p

2nd arg: q

try run your class in command line:

java BinaryGCD 10 10
Drogba
  • 4,186
  • 4
  • 23
  • 31
0

This code expects two arguments to be passed in and when you start the program from within Eclipse you are not passing them in.

The code doesn't make any allowance for a user not passing the arguments, it doesn't try to spot it and give a user-friendly message :-) but just assumes the arguments have been passed

Java has an array 'args' which is automatically populated with these arguments, and so when it tries to look inside the array for these, it throws an ArrayIndexOutOfBoundsException (i.e. you're looking in the array for the 1st and 2nd elements, but the array doesn't have a 1st and 2nd element)

The answers above explain how you can pass arguments in using Eclipse.

matt freake
  • 4,877
  • 4
  • 27
  • 56