• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem reading data from file, putting it into map, then writing it back to text doc.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason when it writes to the text it does not loop through the original map details. here is all the code I think is important


Original data
a a (111)111-1111 b b (222)222-2222 c c (333)333-3333 e e (555)555-5555
data after d d (444)444-4444 is added
e e (555)555-5555 e e (555)555-5555 e e (555)555-5555 d d (444)444-4444 e e (555)555-5555
When I comment out the person object and the value and uncomment the string and key it will display write like this
aabbccddee
I wouldn't normaly post to something like this but I have no clue as to why this is happening.
thankyou
Tara

 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tara,

It's a good idea to use code tags when you're posting code. (Just hit that Code button above the text area.) I've added them for you this time. They make the code much easier to read, although in this case, the tabs are little wonky, so it's still a bit tough.

As for your code, I don't see anything in your readFile() method that reads the phone numbers, so that's why they're not there when you go to write. Also, I'm confused by the beginning, not-in-a-method code snippet. You instantiate a TreeMap on line 3, but immediately reassign the variable to the result of readFile() on line 4. There's no reason for that. Then in lines 5 - 9, you create a new Person, and add it to the already populated TreeMap. Why?

 
tara blurton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry I should have put comments in. I put that the map = readFile() because readFile() returns a map. Also the new person object is a new entry to the map. I just don't see why it writes the e entry 3 times instead of once. and why does it work with the keys but not the values. This does not make sense if it works for one it should work for the other.
new code with comments
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, gotcha. In readFile(), you need to make a new Person object for each record you read from your file. You are now putting the same object in the TreeMap three times, so all you see is the last set of values you set into that object.
 
tara blurton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thankyou so much I guess thats why it worked for the key. Man, if I am going to do this I have to get back into thinking outside of the box.
thanks again. I am working on this program to help get an internship.
Tara
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. Good luck with the internship!
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic