aspose file tools*
The moose likes Beginning Java and the fly likes need help reading text file into hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help reading text file into hashmap" Watch "need help reading text file into hashmap" New topic
Author

need help reading text file into hashmap

sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7
Hey there. I've been given a project to due that involves working with textfiles.
The text files are given in the following structure

ID1<>NAME1
ID2<>NAME2
ID3<>NAME3

Supposedly I'd have a separate class that does I/O and Hashmaps. I have an idea with the i/o codes but don't know how to deal with hashmaps.
I need to display the NAME part only in a text area without the ID or <>, so I should have a method converts the TXT file into hashmap, and then return the name part as an array?
Any help as appreciated !

Viktor Kubinec
Ranch Hand

Joined: Jan 28, 2012
Posts: 34
just read line by line, store id and name as a String and do something like this :



and than for every line



for looping through hashmap you can do this :



for more just google for hashmap and collections tutorials, there are lots of these out there
sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7
thanks for the response. I'll try it out, but most likely will run into more questions
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7793
    
  21

sam t. yang wrote:Hey there. I've been given a project to due that involves working with textfiles.
The text files are given in the following structure:
ID1<>NAME1
ID2<>NAME2
ID3<>NAME3

I presume you mean that the lines have that format.

If so, I'd also look at String.split() for extracting the information from the lines.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7


now that's what i have so far... but need to display the results onto my main class. any help with that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38844
    
  23
Welcome to the Ranch (sorry it’s late) and have a look at the Java Tutorials, which tell you how to iterate the entries or keys or values in a Map.
sam t. yang
Greenhorn

Joined: Feb 23, 2012
Posts: 7
thanks for all the responses. I have made much progress
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help reading text file into hashmap