I have a text file with some data like this
A+,John,James,Henry
B+,Andrew,Chris,Andy
O,Grant,Celina,Claudia
I use buffered reader to load the text into a Treeset
of string, and print the set.
Next I want to separate the names followed by the key i.e.
John A+
James A+
Henry A+
Andrew B+
etc.
How can I assign the key to each of the names separately? I tried using split to separate the strings by commas, but I couldn't get it to work. I tried doing:
TreeSet<String> set1 = loadSetFromFile( infile2 );
and then making a new list and using split to separate the commas with this line
List<String> splitlist = Arrays.asList(set1.split(","));//dont work
Obviously not the correct thing to do. I was thinking that maybe I would have to do it in my set loading method
static TreeSet<String> loadSet( BufferedReader infile ) throws Exception
{TreeSet<String> set = new TreeSet<String>();
while( infile.ready() )
set.add(infile.readLine());//split(",") lol
infile.close();
return set;
}
I would then have to somehow associate the first letter of the line with all of the following. Don't know how to do that either. Any help would be greatly appreciated.
I think you're over-complicating this.
Presuming you have a file in
my/path
namedfoo.txt
containing the text in your example, here's a way to process your data, with a simpleMap
andString.split
, Java 6 style:Output