Array list usage

I keep getting the following error:

java.lang.ArrayIndexOutOfBoundsException: 9

at JayScout.option2(JayScout.java:99)

at JayScout.main(JayScout.java:46)

the code is supposed to accept 10 user entries, with each entry consisting of the following data: name, age, position and average

after accepting the data, the program should sort the entries based on age. After sorting, it should only display the records where age < 25 and the average > 0.280

here is the program code

import java.awt.*;

import hsa.Console;

publicclass JayScout

{

static Console c;// The output console

static String n[] =new String[9];

staticint o[] =newint[9];

static String p[] =new String[9];

staticdouble a[] =newdouble[9];

staticint x;

publicstaticvoid main (String[] args)

{

c =new Console ();

int choice;

c.println("Blue Jay Scout Program\n");

do

{

c.println("\n\n\t\tProgram Menu\n");

c.println("\t\t 1. Store Information");

c.println("\t\t 2. Possible Drafts");

c.println("\t\t 3. Exit");

do

{

c.print ("\n\nWhat is your choice (1-3): ");

choice = c.readInt ();

if ((choice < 1) || (choice > 3))

{

c.setTextColor(Color.red);

c.println ("\nInvalid ... enter 1-3 only\n");

c.setTextColor(Color.black);

}

}

while ((choice < 1) || (choice > 3));

switch (choice)

{

case 1: option1();break;

case 2: option2();break;//this is also highlighted by debugger

}

}

while (choice != 3);

c.println("\n\nBye");

// Place your program here. 'c' is the output console

}// main method

publicstaticvoid option1()

{

c.println("Information Storage\n");

for (x=0;x<9;x++)

{

c.print("\nEnter Player Name - ");

n[x] = c.readLine();

c.print("Enter Player Age - ");

o[x] = c.readInt();

c.print("Enter Player Position - ");

p[x] = c.readLine();

c.print("Enter Batting Average - ");

a[x] = c.readDouble();

}

int y;

int temp;

temp = 0;

for (x = 0; x >= 8; x++)

{

for (y = 0; y <=8-x; y++)

{

if (o[y] > o[y + 1])

{

temp = o[y];

o[y] = o[y + 1];

o[y + 1] = temp;

}

}

}

}

publicstaticvoid option2()

{

c.println("Draft Possibilities\n");

c.println("NAME AGEPOSITIONAVERAGE");

for (x = 0; x<=9; x++)

{

if ((o[x]<25)&&(a[x]>=0.280))// this is where the Debugger pointed out the error is

//so the error is here but i dont know how to fix it

{

c.println(n[x]+""+o[x]+""+p[x]+" "+a[x]);

}

}

}

}

[5807 byte] By [irutaviasa] at [2007-11-27 11:58:41]
# 1

Array indexing in Java begins at zero not one. See this

int[] x = new int[9];

x[0]// entry number 1

x[1]// entry number 2

x[2]// entry number 3

x[3]// entry number 4

x[4]// entry number 5

x[5]// entry number 6

x[6]// entry number 7

x[7]// entry number 8

x[8]// entry number 9

x[9]// entry number 10 - Does NOT exist!

cotton.ma at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 2

can you please tell me where the starting index = 1? all the x values are initiated at 0....

how come number 10 does not exist?

irutaviasa at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 3

because you didnt init the array to be on length 10...

int[] blee = new int[10]; // 10 blees!

mkoryaka at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 4

static String n[] = new String[9];

static int o[] = new int[9];

static String p[] = new String[9];

static double a[] = new double[9];

i put a 9 within the [ ] because there are 10 entries in total, and 0-9 makes 10....or is that wrong?

i require 10 entries/x values

but i tried out what you said,

i put 10 in the [ ]

and the program worked...

THANKS!

but i dont know why

Message was edited by:

irutavias

irutaviasa at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 5

> static String n[] = new String[9];

>static int o[] = new int[9];

> static String p[] = new String[9];

>static double a[] = new double[9];

> put a 9 within the [ ] because there are 10 entries

> in total, and 0-9 makes 10....or is that wrong?

Wrong. In the following line of code the 9 represents the LENGTH of the array. Aka the total number of slots/buckets/elements the array will hold.

double[] a = new double[9];

If you want an array with 10 spaces then you need to say 10 spaces.

double[] a = new double[10];

cotton.ma at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 6

because what you put into the [ ] is the actual size on the array you want, in your case you want it to be 10 long, so thats what you put into there.

rule of thumb is that the initializer is NOT a 0-based index, but the size. the size is always 1 greater then the 0-based index.

mkoryaka at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 7

lol you replied before i could edit my message and thank you guys

thanks...cant believe i was so stupid

irutaviasa at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 8

> lol you replied before i could edit my message and

> thank you guys

>

> thanks...cant believe i was so stupid

Glad you understand it now. There are only stupid people not stupid questions so don't feel bad about making mistakes that later seem silly. It's all part of the learning process.

cotton.ma at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...
# 9

On a separate note, this is bad for a couple of reasons:

static String n[] = new String[9];

static int o[] = new int[9];

static String p[] = new String[9];

static double a[] = new double[9];

* Your member variables should probably be private.

* Those names are not descriptive. For instance, I assume n is an array of names. Call it names, not n.

* If n[ i ], o[ i ], p[ i ] and a[ i ] are all related attributes of one thing--say of a person, then you should create a class called Person that has those as member varaibles. What you have here is very non-OO and defeats the purposeof using an OO language like Java.

jverda at 2007-7-29 19:21:13 > top of Java-index,Java Essentials,Java Programming...