kalyanitokuri
if i run this prog, i got exeption in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at Maxof2.main(Maxof2.java:9), whats the resolution for ths?
1 Answer(s)      4 years and 3 months ago
Posted in : Java Beginners

class Maxof2{

public static void main(String args[]){

  //taking value as command line argument.

  //Converting String format to Integer value

  int i = Integer.parseInt(args[0]);

  int j = Integer.parseInt(args[1]);

  if(i > j)

      System.out.println(i+" is greater than "+j);

  else

      System.out.println(j+" is greater than "+i);

}

}

Advertisement
View Answers

January 6, 2011 at 4:53 PM


Hi Friend,

ArrayIndexOutOfBoundsException:0 means that you haven't specify the arguments along with run command.

Run your code with the following statement:

java Maxof2 5 6

When you will execute your code with the above statement then you will get the output, otherwise you will get the ArrayIndexOutOfBoundsException.

We have specified 5 and 6 as arguments. You can take any number but you have to specify only two numbers.

If you want another way of finding the greatest among two numbers then you can try the following code also.

import java.util.*;
class Maxof2{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter Number 1: ");
 int i = input.nextInt();
 System.out.print("Enter Number 2: ");
 int j = input.nextInt();
 if(i > j)
   System.out.println(i+" is greater than "+j);
 else
   System.out.println(j+" is greater than "+i);
}
}

Thanks










Related Tutorials/Questions & Answers:
Advertisements