This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Overwriting Objects in ArrayList

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Marshal
Posts: 22460
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jalal Albasri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 74076
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to JavaRanch.
 
Rob Spoor
Marshal
Posts: 22460
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
permaculture is a more symbiotic relationship with nature so I can be even lazier. Read tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic