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?

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?

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);

}

}

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

Ads









Related Tutorials/Questions & Answers:
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?
resolution for prog
Advertisements
when I run exiting project that time I got error, I want solution on it?
I GOT ERROR FOR MY PROJECT
i got an error while compile this program manually.
i got an exception while accept to a jsp
i have problem with this query... please tell me the resolution if this .........
i have problem with this query... please tell me the resolution if this .........
i got illegal start of expression error , plese tell me the mistake where i did...
i have got error while compiling simple tag handler
Jsp error when i run this a jsp file
this is my code but i cant able to run it wt is the problem
this is my code but i cant able to run it wt is the problem
Can I have multiple main() methods in the same class?
can i know the error in this code... am unable to run this code
How to Differenciate Main Thread and Child Thread in Java
hi, Everyone ,i'm learning jdbc, i got an nullpointerexception when execute this program.
exception in thread main while running servlet
how can i run ASP.Net Server from netbeans IDE?
i have installed oracle 10g (both ee and xe) editions... while am installing i found port num as 1521 for ee, 1522 for xe
Want to add a resource in the build directory that's created when i run an mvn assembly:assembly command. Pls help!
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
write data to a pdf file when i run jsp page
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
how can i run tomcat server and my home page come when i double click on an icon in servlets
I am new to java applets. When i run this code on applet viewer status is displayed that applet not initialized
exception in thread main java.lang.unsupportedclassversionerror in eclipse

Ads