This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parsing" Watch "parsing" New topic


mike proger

Joined: Apr 22, 2005
Posts: 14
how do I parse data from a text file into a hashmap
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
It's really difficult to give an answer to ur question, mike, since you didn't post ur detailed requirement here. You can parse your file using I/O API. But after that, what do you want to do with your HashMap? What do you want the keys in HashMap to be? And the value?

We need detailed requirement from you.

Co-author of SCMAD Exam Guide, Author of JMADPlus
mike proger

Joined: Apr 22, 2005
Posts: 14
I/O, I will read the text file, put the information into a hashmap, when the program is done I will use the updated hashmap (users may add records) to write the information back to the text file on disk. For the hashmap I will be using account number and key field if you were asking about that.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
In addition, you really need to know something about the format of the file. Text files can be in many different formats, and there's no one answer for all of them.

"I'm not back." - Bill Harding, Twister
mike proger

Joined: Apr 22, 2005
Posts: 14
text file is accounts.txt
uses comma as delimiter
looks something like
1,joe smith,5000
2,john doe,3000
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Learn to use a StringTokenizer. Its easy.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
So you can read your file in the way that your file reader reads the lines in the file one by one and put the whole line into the HashMap as a value and account number as a HashMap key. As the previous poster suggested, use StringTokenizer to cut the tokens, which is delimited by a comma.

Hope this helps.
mike proger

Joined: Apr 22, 2005
Posts: 14
Ok I still need a little help. I was looking at the following example:

import java.util.*;
public class App {
public static void main(String[] args) {
String path = "c:/temp/students.txt";
try {
FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
StringTokenizer parser = new StringTokenizer(line, ",");
ArrayList fields = new ArrayList();
while (parser.hasMoreTokens()) {
System.out.print(fields.get(1) + " " + fields.get(0));
System.out.print(" (" + fields.get(3) + ") ");
line = br.readLine();
System.out.println("End of file");
catch (IOException err) {

Ok so now I have the tokens which are objects right. I need to take the tokens and convert them into a account object which accepts an int, string, and double. And then I have to put that object into a hashmap. Can I get some help for that.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
we seem to be in the same class, i recognize this assignment.

the ArrayList in your example code will hold a series of three Objects, which are really Strings, but the ArrayList doesn't know that. the Account class which (if i'm right about your being in my class) we were given, you might notice, has a constructor which takes an int, a String, and a double.

so, you'll have to access the three elements of the ArrayList, convert them from Objects to the data types they each should be, and then use the Account constructor with that data. the first step is to cast them into Strings:

then from there, you convert the first and last of the strings to integer and double, respectively. creating the Account object should be fairly straightforward once you've read the source code for for clues on how to add the Account object to the HashMap, read the API docs for the HashMap class.

one hint: think of how you might want to go about getting any given Account object back out of the HashMap again — that'll give you an idea about what to use for a key in the HashMap.
[ April 23, 2005: Message edited by: M Beck ]
Consider Paul's rocket mass heater.
subject: parsing
Similar Threads
Statement and PreparedStatement question?
XML attribute/element question?
XML parsing
WA #1.....word association