help reading text into hashmap! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

help reading text into hashmap!

hamboogerhambooger Posts: 2Member
Hey everyone. I'm at the point where in this program I need to read a textfile and place the entries into a hashmap.
this is the txt example
1<>John Kane
2<>Jane Lyman
3<>Peter Hansen
4<>Cathy Harris

I've designed the main app which is the GUI. I have several text areas where I need to display the name only.

On a separate class I need to have it handle the I/O, with buffered reader and read each text files line by line and place them into a hashmap skipping the "<>." Then some how have it displayed in the text areas... can anyone help me out on this?


Comments

  • WirusWirus Posts: 10Member
    Just one of possible ways:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.HashMap;
    import java.util.Map;



    public class Test {
    public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader( new FileReader( "E:/test.txt" ));
    String line = null;
    Map result = new HashMap();
    while( (line = reader.readLine()) != null ){
    String[] arr = line.split( "<>" );
    result.put( arr[0], arr[1] );
    }
    }

    }

  • WirusWirus Posts: 10Member
    Just one of possible ways:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.HashMap;
    import java.util.Map;



    public class Test {
    public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader( new FileReader( "E:/test.txt" ));
    String line = null;
    Map result = new HashMap();
    while( (line = reader.readLine()) != null ){
    String[] arr = line.split( "<>" );
    result.put( arr[0], arr[1] );
    }
    }

    }

Sign In or Register to comment.