Results 1 to 4 of 4
- 06-18-2010, 10:36 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 16
- Rep Power
- 0
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
here is the output from jcreatorJava 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 ); }
the line 66 error is just the access of readbit ();Java Code:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at subnet.readbit(subnet.java:81) at subnet.main(subnet.java:66)
thanks in advance :D
- 06-18-2010, 10:58 PM #2
Member
- Join Date
- Aug 2009
- Posts
- 76
- Rep Power
- 0
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.
- 06-18-2010, 11:10 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 16
- Rep Power
- 0
:confused: so wat should count equal for an array of two then or wat would you do different
:cool:XML Code:<a href="http://www.ubuntu.com/">ubuntu ftw</a>
- 06-18-2010, 11:25 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 16
- Rep Power
- 0
got it heres my revised code to help others
thx again manJava 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 ); }:cool:XML Code:<a href="http://www.ubuntu.com/">ubuntu ftw</a>
Similar Threads
-
[SOLVED] Array index out of bounds exception
By sruthi_2009 in forum New To JavaReplies: 5Last Post: 11-24-2010, 11:46 AM -
array Index out of Bounds exception== 0
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-11-2010, 04:02 PM -
Array Index Out Of Bounds and Problem in Assigning Values
By chronoz1300 in forum New To JavaReplies: 2Last Post: 12-28-2009, 07:14 PM -
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM -
Array Index out of bounds
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 05:11 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks