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









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 have got error while compiling simple tag handler
i got illegal start of expression error , plese tell me the mistake where i did...
Jsp error when i run this a jsp file
I got build failed when deploying enterprise application - EJB
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 know the error in this code... am unable to run this code
Can I have multiple main() methods in the same class?
hi, Everyone ,i'm learning jdbc, i got an nullpointerexception when execute this program.
How I call the function union from the main - Java Interview Questions
how can i run ASP.Net Server from netbeans IDE?
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
How to Differenciate Main Thread and Child Thread in Java
Want to add a resource in the build directory that's created when i run an mvn assembly:assembly command. Pls help!
exception in thread main while running servlet
Main Thread and Child Thread
I am new to java applets. When i run this code on applet viewer status is displayed that applet not initialized
i have installed oracle 10g (both ee and xe) editions... while am installing i found port num as 1521 for ee, 1522 for xe
Exception in thread "main" java.lang.ClassCastException
Exception in thread "main" java.lang.ClassCastException
exception in thread main java.lang.unsupportedclassversionerror in eclipse
exception in thread main java.lang.unsupportedclassversionerror in ubuntu
i/o
i/o
i/o
i/o
my server is giving me erro HTTP Status 404 - /SimpleServelet/ wen i try to run the srvelet
my server is giving me erro HTTP Status 404 - /SimpleServelet/ wen i try to run the srvelet
my server is giving me erro HTTP Status 404 - /SimpleServelet/ wen i try to run the srvelet
my server is giving me erro HTTP Status 404 - /SimpleServelet/ wen i try to run the srvelet
How to launch my web application from my desktop without opening Netbeans IDE everytime i run the application?
i/o
Scala Shell in Spark - How do I run Scala in spark shell?
difference between main thread and child thread?
Java Thread : run() method

Ads