how to insert values into an array of class

Hello everyone,

I need help in inserting values into an array of class, which i have read from a file.

Length of the array is 5. I should insert values one by one into that array.

If the array is full (if count = 5), then I should split the array into 2 arrays

and adjust the values to left and right with median.

I'm getting an ArrayBoundException .. can anybody help me out ?

Thanks in advance

Here is my code..........

import java.util.*;

import java.io.*;

publicclass Tree

{

static String second;

static String first;

staticint count = 5;

publicvoid insert(int f1,int s1,int c)

{

if(c!=0)

{

Record[] rec =new Record[4];

for (int i = 0; i < 5; i++)

{

rec[i] =new Record();

}

for(int i = 0; i<=4;i++)

{

rec[i].x = f1;

rec[i].y = s1;

}

}

else

{

System.out.println("yes");

}

}

publicstaticvoid main(String[] args)

{

Tree t =new Tree();

try

{

FileReader fr =new FileReader("output.txt");// open file

BufferedReader br =new BufferedReader(fr);

String s;

while((s = br.readLine()) !=null)

{

StringTokenizer st =new StringTokenizer(s);

while(st.hasMoreTokens())

{

first = st.nextToken();

second = st.nextToken();

//System.out.println("First-->"+first+" "+"Second-->"+second);

}

int fir = Integer.parseInt(first);

int sec = Integer.parseInt(second);

t.insert(fir, sec, count);

}

fr.close();// close file

}

catch (IOException e)

{

System.out.println("Can't read file");

}

}

}

class Record

{

publicint x,y;

}

[3920 byte] By [qwedwea] at [2007-10-2 21:54:33]
# 1

Hi qwedwe.

Record[] rec = new Record[4];

for (int i = 0; i < 5; i++)

{

rec[i] = new Record();

}

Here is your error: you have an array of 4 Records, but you create and (try to) insert 5 Record-instances.... try:

Record[] rec = new Record[c];

for (int i = 0; i < c; i++)

{

rec[i] = new Record();

}

Regards,

Norman

Waldmeister_iiia at 2007-7-14 1:10:30 > top of Java-index,Other Topics,Algorithms...