Results 1 to 4 of 4
  1. #1
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Default "java.lang.ArrayIndexOutOfBoundsException:" Problem...

    Hello,

    I'm receiving the folllowing error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50
    at numberCounter.main(numberCounter.java:32)

    when attempting to run this code:

    Code:


    int i, j, n, max=50, number, t;
    boolean located;
    int [] nums =new int[50];
    int [] count =new int[50];

    System.out.print("How many numbers do you plan to input??");
    n = Keyboard.nextInt();

    for(i=0;i<n;i++)
    {
    System.out.print("Enter number " +(i+1)+": ");
    number = Keyboard.nextInt();
    located = false;

    for(j=0;j<max;j++)
    if(nums[j]==number)
    {
    located=true;
    count[j]++;
    System.out.println(j+" " + count[j]);
    }
    if(!located)
    {
    nums[max]=number;
    count[max]=1;
    max++;
    }
    }

    System.out.println( +max+ " different numbers were entered");
    System.out.println("In order from largest to smallest they are as follows: ");

    for(i=0;i<max-1;i++)
    for(j=i;j<max;j++)
    if(nums[i]<nums[j])
    {
    t=nums[i];
    nums[i] = nums[j];
    nums[j] = t;
    t = count[i];
    count[i] = count[j];
    count[j] = t;
    }

    System.out.println("N\t Count");
    for(i=0;i<max;i++)
    System.out.println(nums[i]+"\t"+count[i]);

    }
    }

    End of Code:

    Thanks,

    Murd

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...

    For an array with a size of 50 the valid indicies are 0 - 49. You are trying to access element 50 which does not exist. Debug your code to find out why and correct it.

  3. #3
    Murd is offline Member
    Join Date
    Nov 2011
    Posts
    8
    Rep Power
    0

    Post Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...

    I think I understand what you're saying. Regardless it compiles correctly with the corrections I made. I appreciate your help.

    Thanks,

    Murd

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: "java.lang.ArrayIndexOutOfBoundsException:" Problem...

    It might compile but AIOOBEs are generated at runtime.

Similar Threads

  1. Replies: 3
    Last Post: 10-20-2011, 04:58 PM
  2. Replies: 3
    Last Post: 02-04-2011, 08:31 PM
  3. Replies: 3
    Last Post: 05-13-2010, 05:07 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 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
  •