aspose file tools*
The moose likes Beginning Java and the fly likes Overwriting Objects in ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overwriting Objects in ArrayList" Watch "Overwriting Objects in ArrayList" New topic
Author

Overwriting Objects in ArrayList

Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
Hi, so im having trouble with adding objects to arraylist or rather adding unique objects to arraylist.. after the second one is added the first one is overwritten.. which i know means ive only created one object but im calling the constructor before reading the values for the second object.. i hope my explanation makes sense...

the program is simply meant to read peoples details from a text file create objects and add them to the arraylist

Thanks for any help guys!

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Why is this line there:

This will overwrite the line Scanner in each iteration of the inner loop. In other words, your code will do the following:
1) read the first line of the file
2) create a Scanner object of it
3) read the first value of the line
4) add it to the Person object
5) read the next line of the file if present
6) go to step 2

If you remove this one statement completely, I think you will get what you want: one Person object will be created for each line in the file.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
Hey thanks for replying so quick..

Its there cos the input file looks something like this.. so i want to keep the same person object until it hits a blank line the the second loop will terminate begin with the first loop get the next line with data and create a new object.. so basically that liens there to get the next line so that the second loops conditional will be false if the line is blank..

Input file..

name jones
phone 12345
email indiana@gmail.com
annadale st.,
annandale,
nsw

name dude
phone 111111
email valid.eeee@gmail.com
address 2 Finite Parade
My Suburb
9998
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
. . . and welcome to JavaRanch.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

So basically, what you need is:

For checking the line, and reading lines, you will need to use the same Scanner object. Only create a new Scanner for each line to read data from the line itself; it's the "do something with the line" part.
Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
Hi, So i've changed the looping only one now.. and changed where the object is created but still the same problem, after the data for the second record is read the data for the first record is changed as well..

when i print the object i get a two different references Person@a05308 and Person@ab50cd.. does this mean i am creating two objects or not necessarily...



public static ArrayList<Person> readData(String path)
{
/* Go through the input file, create objects for each person,
Assign values to their variables and add them to the ArrayList*/
try
{
File source = new File(path);
Scanner in = new Scanner (source);
ArrayList<Person> book = new ArrayList<Person>();
int i = -1;

while (in.hasNext())
{
//Create second scanner object, a line
Scanner line = new Scanner(in.nextLine());
String token;

//Collect data from record
if (line.hasNext())
{
token = line.next();

if (token.equals("name"))
{
i++;
book.add(new Person());
book.get(i).name = line.next();
while(line.hasNext())
book.get(i).name += " " + line.next();
}
else if (token.equals("phone"))
{
book.get(i).phone = Integer.parseInt(line.next());
}
else if (token.equals("email"))
{
String e = line.next();
if(validateEmail(e))
{
book.get(i).email = e;
}
}
else if (token.equals("address"))
{
book.get(i).address = line.next();
while(line.hasNext())
book.get(i).address += " " + line.next();
}
else
{
book.get(i).address += "\n" + token;
while(line.hasNext())
book.get(i).address += " " +line.next();
}
}
}
System.out.println(book.get(0).name);
return book;
}

catch (FileNotFoundException e)
{
System.out.println("file not found");
return null;
}
}
Jalal Albasri
Greenhorn

Joined: May 17, 2008
Posts: 8
oh my gawd.. i just found what was going wrong.. i had declared my instance variables as static.. f* thats frustrating..

thanks for all you're help though guys..

pce.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overwriting Objects in ArrayList