• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with arrays

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. thanks a lot. =)
 
That's a very big dog. I think I want to go home now and hug this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic