I am reading from a file and while i know how to work with those i am trying to read the line. get all the fields. then store them in a class with those fields. then store that object in a vector.
Heres how i deal with reading the stuff from the file:

StringTokenizer tokens;
String field1,field3;
int field2;
BufferedReader myFile = new BufferedReader(new Filereader("textfile.txt"));
String str1 = myFile.readLine();
while (str1 != null){
tokens = new StringTokenizer(str1); 
 while(tokens.hasMoreTokens()){
   field1 = tokens.nextToken();
   field2 = Integer.parseInt(tokens.nextToken());
   field3 = tokens.nextToken();}
   str1 = myFile.readLine();
 // THIS is where i need code to set values on the Class Fields
//read below for more explanation 
}

Now thats just sample code i just typed to show that im not trying to get help without posting some code.

Next i have a class

class Fields{
String name,lastname;
int age;
public void setage(int myAge){
age = myAge;
}
public int getage(){return age;}
//i have more methods to get to get and set the fields in this class. 
// i also have a 3 arg constructor and a no arg constructor. 
}

Now here is my question. How can i make it so after i read all those fields. set them to a new fields object then place all of them in a vector. There is going to be more than one line in the text file.

class doStuff{

Vector<Fields> fieldVec = new Vector<Fields>();
}

Now i want to read from the file in the main method.which i posted sample code up up above. Then initialize a Fields object with those fields. then store it in a vector.

Recommended Answers

All 4 Replies

Here is how i think it could be done.

StringTokenizer tokens;
String field1,field3;
int field2;
BufferedReader myFile = new BufferedReader(new Filereader("textfile.txt"));
String str1 = myFile.readLine();
while (str1 != null){
tokens = new StringTokenizer(str1); 
 while(tokens.hasMoreTokens()){
   field1 = tokens.nextToken();
   field2 = Integer.parseInt(tokens.nextToken());
   field3 = tokens.nextToken();}
   str1 = myFile.readLine();
   Fields FieldsA1 = new Fields(field1,field2,field3);
   // here i pass the values to the 3 arg constructor. But when i go to the next line
   // this wont work because it will overwrite it since its name is still Fields
// now since i do this in a while loop this will get overwritten. is there any way i 
// can code it so new objects get created for each line and they get populated by 
// the fields?


}

Ok after reading more and more i have simplified to what i need to do. I need to create multiple objects of type Fields and store them in a vector. So every time i read a line. i create a new object by passing the fields to the 3 arg constructor then move on to the next line.

That third post sounds about right, though I admit I haven't read all of your code. Let us know if you have any trouble. Welcome to the forums

:)

Ok well i am adding stuff to the vector but when i go to print it i get Fields@(some random text string here)

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.