Results 1 to 4 of 4
  1. #1
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default Array Index Out Of Bounds Exception

    hey could someone help me with this im new and am trying to make a program to work out subnet addresses i have made a class called read bit which i would like to take a reading of bit[0] and bit[1] here is the code
    Java Code:
    static void readbit() {
    
    int count = 2;
    
    double bit[] ;
    bit = new double [count];
    
    do {
    
    	System.out.print ("\n\nPlease input  octet "+count+1+" :");
       	bit[count]= Keyboard.readDouble();
       	count++;
    
    } while (count < 2 );
    
    }
    here is the output from jcreator

    Java Code:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    	at subnet.readbit(subnet.java:81)
    	at subnet.main(subnet.java:66)
    the line 66 error is just the access of readbit ();

    thanks in advance :D

  2. #2
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    The issue you have is that arrays are 0 based. Basically you are saying that you want bit[2] to equal whatever is inputted via the Keyboard. But earlier you created your bit array of size 2.

    So your array is only able to store objects at positions 0 and 1. When you try to store something at position 2, its outside of the boundaries of the array.

  3. #3
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default

    :confused: so wat should count equal for an array of two then or wat would you do different
    XML Code:
    <a href="http://www.ubuntu.com/">ubuntu ftw</a>
    :cool:

  4. #4
    manowar689 is offline Member
    Join Date
    Jun 2010
    Posts
    16
    Rep Power
    0

    Default got it heres my revised code to help others

    Java Code:
    static void readbit() {
    
    int count = 0;
    
    double bit[] ;
    bit = new double [2];
    
    do {
    
    	System.out.print ("\n\nPlease input the price of "+count+1+" :");
       	bit[count]= Keyboard.readDouble();
       	count++;
    
    } while (count < 2 );
    
    }
    thx again man
    XML Code:
    <a href="http://www.ubuntu.com/">ubuntu ftw</a>
    :cool:

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 11:16 AM
  2. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  3. Array Index Out Of Bounds and Problem in Assigning Values
    By chronoz1300 in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 08:14 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM
  5. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 06:11 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
  •