I am reading from a file. After i tokenize and assign each value to a string field i call the constructor and initialize the object. next i want to add them to an array. I have done this with a vector and all i do is

ObjectVec.add(obj1);

Mind you this is all in a while loop. here is the relevant code.

BufferedReader myFile = new BufferedReader(new FileReader("myfile.txt"));
String string1 = myFile.readLine();
//here is where i add obbjects to a vector
String f1,f2,f3;//assume text file has 3 fields as in name last name age
while(string1!=null){
  token = new StringTokenizer(string1);
  while(token.hasMoreTokens()){
    f1 = token.nextToken();
    f2 = token.nextToken();
    f3 = token.nextToken();
    ObjectCreator obj1 = new ObjectCreator (f1,f2,f3);//NOTE this is just code im writing down offhand. to explain the problem im running into. so the names are not relevant
//after i create it i just add the new object to the vector.
ObjectVec.add(obj1); // here thats it. its easy with the vector. next i just vlose the while loop and go tot he next line.
  }
string1 = myFile.readLine();
}
// and thats it. my vector contains all of the objects created.

How would i go about doing this with an array?
Here is what i think to do so far but none of it is working.
First i create the array of type ObjectCreator.
Then i just have the above code and instead of

ObjectVec.add(obj1);

i have

newArray[k]= obj1; //then i incrument k outside the while loop in the same place where i go to the next line.

Now i understand that the while loop will go indefinitely since it will keep going up but that wont really matter since the array will be filled and i have the array large enough as i need then the rest wont matter. Either way is there a better solution to this? Where after i create the object with the arg constructor i add it to the array?

Why not simply use the vector (or an ArrayList if it does need to be synchronized at that point) and then call toArray on that Vector/List?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.