Forums Register Login

help with arrays

+Pie Number of slices to send: Send
i got this error when i ran my program: Exception in thread main java.lang.ArrayIndexOutOfBoundsException
int i;
Acc[] bank = new Acc[i];
bank[i] = new Acc("","",0.0,0.0);
this is how i declared my array. is it legal for me to do it this way? i'm trying to create i amount of arrays where i is the user input.
+Pie Number of slices to send: Send
First point: you need to have Acc be an object that you have already defined in a class named Acc.java. You might have done this OK, but I don't have that information here.
Next point: you must have a value given to i before you can use it to define the size of your array you named "bank". You might have done this OK too, but I don't have that information here.
Lastly, and here you HAVE a problem: array of size i has indexes 0 to (x-1). There is no "i" index, the "i"th is index (x-1). To have an "i" index you need an array of size (i+1).
[ April 09, 2004: Message edited by: Elouise Kivineva ]
[ April 09, 2004: Message edited by: Elouise Kivineva ]
+Pie Number of slices to send: Send
Normally, you declare an array like this:

Now x is an int[] of size 5, meaning that I could assign values to indices 0 (inclusive) through 5 (exclusive):

So what you have done is declare an integer i, which is initialized to zero by default, create an array based on it of size zero, and then try to store something it in. Since it has no valid indices, it's going to throw that exception.
sev
+Pie Number of slices to send: Send
thanks for replying. anyway i have to accept user input for the size of the array so how do i do it?
int i;
//assume buffered reader code and class Acc is already there
i = console.readLine();
Acc[] bank = new Acc[i];
bank[i] = new Acc("","",0.0,0.0);
i tried the above way but it still gives me the same error.
+Pie Number of slices to send: Send
hi tan kian
First of all, you should check whether the value i is bigger than the default array or not, then decide to create the array, if the value is smaller than the default array, then use exception handler to handle the exception, if you want to use the default size of array.
It may be better if the array is created from the input value, not hard-code the size and value of array.

[ April 09, 2004: Message edited by: siu chung man ]
+Pie Number of slices to send: Send
 

Originally posted by siu chung man:
It may be better if the array is created from the input value, not hard-code the size and value of array.


thats what i am trying to do, but not succeding in doing so far. what am i doing wrong?
+Pie Number of slices to send: Send
int i;
//assume buffered reader code and class Acc is already there
i = console.readLine();
Acc[] bank = new Acc[i];
bank[i] = new Acc("","",0.0,0.0);
i tried the above way but it still gives me the same error.
----------------------------------------------------------------------
You get an array out of bounds error because an array of size i has indexes 0 to (i-1) and you are trying to do something to index i. There is no index i!
Try
i = console.readLine();
Acc[] bank = new Acc[i];
bank[(i-1)] = new Acc("","",0.0,0.0);
+Pie Number of slices to send: Send
it works, thanks. but i'm still a bit confused.
i = console.readLine(); //assume its 5
Acc[] bank = new Acc[i]; //creates 5 new bank accs
bank[(i-1)] = new Acc("","",0.0,0.0); //creates acc 0 - 4
is that the way its working?
+Pie Number of slices to send: Send
Originally posted by tan kian:
i = console.readLine(); //assume its 5
Acc[] bank = new Acc[i]; //creates 5 new bank accs
bank[(i-1)] = new Acc("","",0.0,0.0); //creates acc 0 - 4


i = console.readLine(); // assume i is set to 5
Acc[] bank = new Acc[i]; // creates an array which can hold 5 new bank accounts
bank[0] = new Acc("a", "", 1.0, 0.0);
bank[1] = new Acc("b", "", 2.0, 0.0);
bank[2] = new Acc("c", "", 3.0, 0.0);
bank[3] = new Acc("d", "", 4.0, 0.0);
bank[4] = new Acc("e", "", 5.0, 0.0);

Here your array named "bank" is initialized to contain five new Acc objects.
[ April 10, 2004: Message edited by: Marilyn de Queiroz ]
+Pie Number of slices to send: Send
ok. thanks a lot. =)
What's a year in metric? Do you know this metric stuff tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 898 times.
Similar Threads
repeted values in jsp
Transferring funds
Nested loops
Help with arrays/class
Aplication Client Container question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:05:51.