aspose file tools*
The moose likes Beginning Java and the fly likes help with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with arrays" Watch "help with arrays" New topic
Author

help with arrays

tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
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.
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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 ]
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
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
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
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.
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
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 ]

Francis Siu
SCJP, MCDBA
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
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?
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154
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);
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
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?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
tan kian
Ranch Hand

Joined: Apr 09, 2004
Posts: 40
ok. thanks a lot. =)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with arrays