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.