Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

    Default ArrayIndexOutofBounds Exception

    Hi,
    I am new to java..


    please help...


    public class atul{
    public static void main(String[] args) {
    System.out.println("Addition of two numbers!");
    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);
    int sum = a + b;
    System.out.println("Sum: " + sum);
    }
    }

    Its giving the "ArrayOutofBounds Exception". Pls help..

    I am using JDK 1.6

  2. #2
    Cemi's Avatar
    Cemi is offline Member
    Join Date
    Apr 2010
    Posts
    36
    Rep Power
    0

    Default

    I believe this is because your array is undefined, you have no element in args[0] and args[1].
    Carpe Diem
    Each day's a gift and not a given right

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Yep.
    What's the command you're calling?
    "java atul" then what?

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Java Code:
    public class atul()
    {
        public static void main(String[] args) {
    
           //Define array of type int
           //Add values to the array
    
            System.out.println("Addition of two numbers!");
    
            int a = args[0];
            int b = args[1];
            int sum = a + b;
            System.out.println("Sum: " + sum);
         }
    }
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Before you execute a program that requires user to input command line parameters, it's always a good idea to add a check to see if they were entered correctly, for example:
    Java Code:
    public class CmdPar {
      public static void main(String[] args) {
        if(args.length == 0) {
          System.out.pritln("Usage: java CmdPar param1 param2");
          System.exit(1);
        }
        //execute the program now that you know args is initialized
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Quote Originally Posted by FlyNn View Post
    Java Code:
    public class atul()
    {
    ...stuff...
    }
    How does that help?

  7. #7
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    How does that help?

    I put comments in to what is needed to make it work... :confused:
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Still doesn't solve his problem, which is that "args" is either empty or has only one argument in it.

  9. #9
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    Before you execute a program that requires user to input command line parameters, it's always a good idea to add a check to see if they were entered correctly, for example:
    Java Code:
    public class CmdPar {
      public static void main(String[] args) {
        if(args.length == 0) {
          System.out.pritln("Usage: java CmdPar param1 param2");
          System.exit(1);
        }
        //execute the program now that you know args is initialized
    }
    I have never seen this sort of thing before. So this is to be used just if you are going to run the file using cmd? At what point args will not get initialized?
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  10. #10
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    if you expect exactly to numbers as arguments then use

    Java Code:
    import java.text.ParseException;
    
    public class atul {
    	public static void main(String[] args) {
    		if (args.length != 2) {
    			System.out.println("Usage: java atul number1 number2");
    			System.exit(1);
    		}
    
    		try {
    			int a = Integer.parseInt(args[0]);
    			int b = Integer.parseInt(args[1]);
    		} catch (NumberFormatException nfe) {
    			System.out.println("You entered invalid numbers, retry.");
    		}
    	}
    }
    the parse method can throw an exception so that's why you should use a try-catch block.
    Last edited by j2me64; 04-09-2010 at 03:57 PM.

  11. #11
    Join Date
    Apr 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. ArrayIndexOutOfBounds
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-07-2009, 12:59 AM
  3. exception
    By mohneesh in forum New To Java
    Replies: 3
    Last Post: 08-16-2009, 02:28 PM
  4. Exception!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-05-2008, 01:39 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •