Mason Smith
Mason Smith

Reputation: 115

How to read a text file with two columns and insert into a treemap?

The idea is to have a text file with information like:

FF0001 Red

FF0002 Blue

FF0003 Yellow

....

To pull this information and store it into a tree map. This is my code so far...

    public static void main(String[] args) {
    File file = new File("test.txt");
    TreeMap<String, String> colors = new TreeMap<String, String>();
           BufferedReader br = null;

    try {
        FileReader fr = new FileReader(file);
        br = new BufferedReader(fr);

        String line;
        String line1;
        while ((line = br.readLine()) != null) {
            String[] splited = line.split(" ");
            for (String part : splited) {
                colors.put(part, part);
            }
        }
        Set<Map.Entry<String, String>> set = colors.entrySet();
        for (Map.Entry<String, String> col : set) {
            System.out.println(col.getKey() + " " + col.getValue());
        }

    } catch (FileNotFoundException e) {
        System.out.println("File does not extist: " + file.toString());

    } catch (IOException e) {
        System.out.println("Unable to read file: " + file.toString());

    } finally {
        try {
            br.close();
        } catch (IOException e) {
            System.out.println("Unable to close file: " + file.toString());
        } catch (NullPointerException ex) {
            // File was never properly opened
        }
    }

My output is:

FF0001 FF0001

FF0002 FF0002

FF0003 FF0003

Red Red

Blue Blue

Yellow Yellow

I am new to java collections and I need the information sorted which is why I choose a treemap, but I cannot seem to figure out why it is storing all information into the key and the value.

Thanks, first time poster here.

Upvotes: 0

Views: 1460

Answers (3)

Sandeep Patange
Sandeep Patange

Reputation: 459

You need to remove the this for loop,

       for (String part : splited) {
            colors.put(part, part);
        }

Instead directly use array index as key and value.

  colors.put(splited[0], splited[1]);

Upvotes: 0

Andrey Pushin
Andrey Pushin

Reputation: 196

You have mistake in code

colors.put(part, part);

So you key and value in map are same. You can write somethink like:

        String[] splited = line.split("\\s+");
        colors.put(splited[0], splited[1]);

And read this answer about split space: How do I split a string with any whitespace chars as delimiters?

Upvotes: 0

SMA
SMA

Reputation: 37053

When you will split your line using space, it will generate two values for e.g. like "FF0001 Red"

it will have

 splitted [0] = "FF0001" //id
 splitted [1] = "Red" //color

and you are trying to store it like:

for (String part : splited) { 
      colors.put(part, part);//see you are storing same key and value so in above it case like key and value entry "FF0001" and another one for color.
}

Instead you need something like:

String id = splited[0];
String color = splited[1];
colors.put(id, color);

Upvotes: 4

Related Questions