Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File parsing

Status
Not open for further replies.

DB2Problem

Programmer
Oct 17, 2002
53
US
Hello ALL

I need to create a hashMap or some kinda data structure of name=value pair after reading these contents from a file

abcd@email.com|1|2|3|4|n|y
efgh@email.com|1|2|3|4|n|y
ijkl@email.com|1|2|3|4|n|y
mnop@email.com|1|2|3|4|n|y

How can i do this in JAVA

Please advise

Thanks a lot
 
From my understanding of HashMap, you can only have one key and one value. In you example I assume that you want to save the email address as a key, the rest of line as a value.

Something likes:
abcd@email.com = |1|2|3|4|n|y
efgh@email.com = |1|2|3|4|n|y
ijkl@email.com = |1|2|3|4|n|y
mnop@email.com = |1|2|3|4|n|y

Is that correct?


Chinese Java Faq Forum
 
No that's not what I want,

I want to separate these all by pipe delimited field and store them separately. Return that datastructure back to calling method for further processing.

The design should be modular rather then storing in separate variable types.

I tried to separate them using StingTokenizer and then gathering them in separate variables BUT then it's still not solve the purpose of returing some kind of datastrure that I can use in calling method where i can get the tokens one at a time..
 
Oh, I misunderstood your question. Here is my solution:
Code:
public class MailAddr{
    private String mail=null;
    public MailAddr(String addr){
        this.mail = addr;
    }

    public String getAddr(){
        return this.mail;
    }
}

public class Info{
    private String v1,v2,v3;
    public Info(String a1, String a2, String a3){
         this.v1=a1;
         this.v2=a2;
         this.v3=a3;
    }
}

..........
..........
//add to hashmap
Hashmap map = new Hashmap();
Mail mail = new Mail("abcd@email.com");
Info info = new Info("1","2","n");
map.add(mail, info);
....
....



Chinese Java Faq Forum
 
What's wrong with a String array? If you don't define how you will access the data in the further processing, it's impossible to give you advice about the data structure.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top