Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
tara blurton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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

 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Good luck with the internship!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!