Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with arrays

 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. thanks a lot. =)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic