Results 1 to 4 of 4
  1. #1
    Csagohan is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Array Out of Bounds Exception

    I have a simple code but when I run it, I get an array out of bounds exception error. I researched other topics but I cant apply it to my problem since im starting out with this.

    Java Code:
    java.lang.ArrayIndexOutOfBoundsException: 0
    	at IntOps.main(IntOps.java:5)
    for this code

    Java Code:
    public class IntOps
    {
        public static void main(String[] args)
        {
            int a = Integer.parseInt(args[0]);
            int b = Integer.parseInt(args[1]);
            int p = a * b;
            int q = a / b;
            int r = a % b;
            System.out.println(a + " * " + b + " = " + p);
            System.out.println(a + " / " + b + " = " + q);
            System.out.println(a + " % " + b + " = " + r);
            System.out.println(a + " = " + q + " * " + b + " + " + r);
        }
    }
    Can anyone help? Thanks.

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

    Default Re: Array Out of Bounds Exception

    get an array out of bounds exception error
    The index being used is not in range.
    Java Code:
    int[] anIntA= new int[1];
    anIntA[1]++;   // java.lang.ArrayIndexOutOfBoundsException: 1
    The valid index is 0.

    The code should test the length of the array BEFORE trying to index into it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Csagohan is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: Array Out of Bounds Exception

    Quote Originally Posted by Norm View Post
    The index being used is not in range.
    Java Code:
    int[] anIntA= new int[1];
    anIntA[1]++;   // java.lang.ArrayIndexOutOfBoundsException: 1
    The valid index is 0.

    The code should test the length of the array BEFORE trying to index into it.
    Thanks but I still don't understand how to use it, (sorry im new to all of this) where do I put this line of code?

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

    Default Re: Array Out of Bounds Exception

    That code was a demo of how to get an array index out of bounds exception.
    Your code assumes that the the array: args has 2 elements. The code needs to test the length of the array BEFORE trying to access the first 2 elements. If the array length is < 2 it should NOT try to access them.
    Test the length, if < 2, print an error message to the user and exit the program because the program needs 2 argument values to execute correctly.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Array index out of bounds exception...
    By NinjaRobotPanda in forum Java 2D
    Replies: 2
    Last Post: 01-30-2013, 01:27 PM
  2. Out of bounds Exception in Array
    By Catto in forum New To Java
    Replies: 6
    Last Post: 12-07-2012, 12:43 PM
  3. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 10:24 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  5. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 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
  •