Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default How to solve ArrayIndexBoundOfException error

    Hi!
    I am a newbie and need some help to know that in the followin' program where i am going wrong:-

    public class userinput
    {
    public static void main(String[] args)
    {
    System.out.println("Enter the value of A: ");
    int a=Integer.parseInt(args[1]);
    System.out.println("Enter the value of B: ");
    int b=Integer.parseInt(args[2]);
    int c=a+b;
    System.out.println("The sum is: "+c);
    }
    }
    Last edited by amityadav9314; 09-01-2010 at 08:04 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by amityadav9314 View Post
    Hi!
    I am a newbie and need some help to know that in the followin' program where i am going wrong:-

    public class userinput
    {
    public static void main(String[] args)
    {
    System.out.println("Enter the value of A and B: ");
    int a=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);
    int c=a+b;
    System.out.println("The sum is: "+c);
    }
    }
    Your program expects two comman line arguments so pass them in otherwise an AIOOB Exception is thrown. Your program could check the number of arguments passed to it by checking the value of args.length.

    kind regards,

    Jos

  3. #3
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Your program expects two comman line arguments so pass them in otherwise an AIOOB Exception is thrown. Your program could check the number of arguments passed to it by checking the value of args.length.

    kind regards,

    Jos
    can you please correct the code for me as I am a total noob, didn't get what you said?
    Last edited by amityadav9314; 09-01-2010 at 07:21 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    where i am going wrong
    Can you say why you think you are going wrong? Do you get errors?
    Please copy and paste the full contents of the error message text here.

  5. #5
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you say why you think you are going wrong? Do you get errors?
    Please copy and paste the full contents of the error message text here.
    I am gettin' a run time error.
    And that is array index out of bound exception
    Last edited by amityadav9314; 09-01-2010 at 07:20 PM.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by amityadav9314 View Post
    can you please correct the code for me as I am a total noob, didn't get what you said?
    Give a man a fish, he eats for a day. Teach a man to fish, he eats for a lifetime.

    The idea behind what JosAH said is that you are accessing args[0] and args[1], however if the program is launched with an empty args[] array, you will get this ArrayOutOfBounds exception.

    So before your code accesses args[0] and args[1], you will want to check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1].

    If you're still having issues implementing this, show us some attempts you've made; and if you still get error messages, posting the FULL error message (copied and pasted) is very helpful.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    When you get errors:
    Please copy and paste the full contents of the error message text here.
    there is useful information in the error message that you have left off.

  8. #8
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    When you get errors:
    Please copy and paste the full contents of the error message text here.
    there is useful information in the error message that you have left off.
    amit@ubuntu:~/Desktop/amit$ javac userinput.java
    amit@ubuntu:~/Desktop/amit$ java userinput
    Enter the value of A:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at userinput.main(userinput.java:6)
    amit@ubuntu:~/Desktop/amit$
    :
    :
    :
    that is the error i am gettin'....

  9. #9
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    So before your code accesses args[0] and args[1], you will want to check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1].

    thanks for tryin' to help/explain me but I really can't get what to do?
    Please explain it more or correct the error.

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Have you learned the concept of if statements?
    Java Code:
    if (...) {
        // Something that will happen if ... is true
    } else {
        // Something that will happen if ... is not true
    }

  11. #11
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Give a man a fish, he eats for a day. Teach a man to fish, he eats for a lifetime.

    The idea behind what JosAH said is that you are accessing args[0] and args[1], however if the program is launched with an empty args[] array, you will get this ArrayOutOfBounds exception.

    So before your code accesses args[0] and args[1], you will want to check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1].

    If you're still having issues implementing this, show us some attempts you've made; and if you still get error messages, posting the FULL error message (copied and pasted) is very helpful.
    Quote Originally Posted by Zack View Post
    Have you learned the concept of if statements?
    Java Code:
    if (...) {
        // Something that will happen if ... is true
    } else {
        // Something that will happen if ... is not true
    }



    yes i guess i have learned the concept of if else statement..

    as you said earlier, "check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1]...."I Don't know how to check that.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    check if args.length is 2
    That is almost the code there. Here is the actual code:
    args.length == 2

  13. #13
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The idea of this particular if statement is to compare two things: args.length and 2. There are several comparison operators which I'm assuming you're familiar with (<, >, <=, >=, ==, and so on).

    Using one of those five operators, you want to check whether or not args.length is greater than or equal to 2. And if it is, you want to execute the rest of your code:
    Java Code:
    // The next four lines should be what executes if args.length is greater than or equal to 2.
    int a=Integer.parseInt(args[0]);
    int b=Integer.parseInt(args[1]);
    int c=a+b;
    System.out.println("The sum is: "+c);

  14. #14
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Your program expects command line arguments do you know what they are?

    run from command prompt
    C:\"userinput.class filepath here" java userinput 34 35
    where userinput.class filepath here is the filepath to your .class file and omit the surrounding ""

    i.e.
    Java Code:
    public static void main(String args[]){
       if(args.length==2){
       System.out.println(args[0]); // displays 34
       System.out.println(args[1]); // displays 35
      
       // Back to your program
       System.out.println("Enter the value of A: "); // not sure why you have this
       int a=Integer.parseInt(args[0]); 
       System.out.println("Enter the value of B: "); // not sure why you have this
       int b=Integer.parseInt(args[1]);
       int c=a+b;
       System.out.println("The sum is: "+c); // should be 69 
      }
    }
    The reason for your ArrayIndexOutOfBounds Exception is that you are trying to access args[] without passing it any values from command prompt therefore args[1] does not exist.
    Last edited by al_Marshy_1981; 09-02-2010 at 02:22 AM. Reason: curly braces

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    And please stop asking people to provide you with the code.

  16. #16
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    And please stop asking people to provide you with the code.
    Well I neva asked for the code, Sir.
    I was just askin' where the hell I was going wrong.
    Now I can understand the problem, but still could not managed to implement that. I am still very new to this programming that's why I could not get those words about "command line arguments". Something still is going wrong, but does not matter there are still two more methods to take the user input that i know...
    1:-using util Scanner
    2:-using buffered reader
    I will get to this problem later.

    thanks y'all for replying me.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Well, you asked a few times for people to correct your code.

    If, after trying the solutions provided, you are still having problems then you post your attempts and explain where it is going wrong (ie compilation errors, or runtime exceptions).

  18. #18
    amityadav9314 is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Well, you asked a few times for people to correct your code.

    If, after trying the solutions provided, you are still having problems then you post your attempts and explain where it is going wrong (ie compilation errors, or runtime exceptions).
    public class userinput
    {

    public static void main(String args[])
    {

    System.out.println("Enter the value of A: ");
    int a=Integer.parseInt(args[0]);
    System.out.println("Enter the value of B: ");
    int b=Integer.parseInt(args[1]);
    int c=a+b;
    if(args.length>=2)
    {
    System.out.println("The sum is: "+c);


    }
    }
    }



    This is what i did later but still I am gettin' a run time error ans that is:-

    "C:\Program Files\Java\jdk1.7.0\bin>java userinput
    Enter the value of A:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at userinput.main(userinput.java:8) "

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Please go back through the posts on this thread. The problem causing the error has been described, analyzed and solved several times.

    Why do you test if the data in args is present AFTER your code assumes that it is present and tries to use it? Move the if() test to before using the args array elements.

    The computer executes the statements in your program in the SAME order that they are entered. It normally starts at the top and moves down. Code at the bottom is done AFTER code at the top.

  20. #20
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    C:\"userinput.class filepath here" java userinput 34 35
    notice the two arguments after userinput?

    You have:
    "C:\Program Files\Java\jdk1.7.0\bin>java userinput
    there are no arguments passed to args here, compare with above

Page 1 of 2 12 LastLast

Similar Threads

  1. plz solve this error
    By silversurfer2in in forum AWT / Swing
    Replies: 14
    Last Post: 06-15-2010, 03:30 PM
  2. Can't Solve Array Error
    By Gregadeaux in forum New To Java
    Replies: 2
    Last Post: 12-14-2009, 03:01 AM
  3. how to solve syntax error
    By pro85 in forum Java Applets
    Replies: 5
    Last Post: 04-06-2009, 11:20 AM
  4. Can't solve error message while looping
    By BHCluster in forum New To Java
    Replies: 15
    Last Post: 04-22-2008, 10:51 AM
  5. Help mi solve my error
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-11-2008, 05:26 AM

Posting Permissions

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