0

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.

varsha
  • 1,620
  • 1
  • 16
  • 29
Mason Smith
  • 115
  • 1
  • 15

3 Answers3

4

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);
SMA
  • 36,381
  • 8
  • 49
  • 73
  • Worked perfectly thank you, trying to get used to manipulating things to get them to what i neeed – Mason Smith Mar 07 '15 at 07:39
  • @Downvoter may i know the reason for the same for right and working answer so i could improve answer next time? – SMA Mar 07 '15 at 10:18
0

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?

Community
  • 1
  • 1
0

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]);
Sandeep Patange
  • 459
  • 6
  • 16