sorting - how to sort and arrange a text file in java to group lines -
really struggling work out...
i have text file data (17000 lines of it)
45226 1 45226 1 45226 1 45226 3 45226 5 23470 1 45226 5 45226 5 29610 4 37417 2 37417 3 37948 1
what want sort text file (using java) left numbers grouped if right value 1. or left value group if right not equal 1 (so other number). example (but doesn't have this)
3 x 45226 1 4 x 45226 mixed 1 x 23470 1 1 x 29610 mixed 2 x 37417 mixed 1 x 37948 1
i know may need use array? or sort of sort? can't work out :'(
any help, code or suggestions - appreciated!
thank you!
my code:
public class sort { private static class counter { private int one; private int mixed; public void incrementone() { one++; } public void incrementmixed() { mixed++; } } public static void main(string[] args) throws ioexception { map<string, counter> map = new linkedhashmap<string, counter>(); string filename = "input.txt"; bufferedreader reader = new bufferedreader(new filereader(filename)); (string line = reader.readline(); line != null; line = reader.readline()) { stringtokenizer tokenizer = new stringtokenizer(line); string key = tokenizer.nexttoken(); string value = tokenizer.nexttoken(); counter counter = map.get(key); if (counter == null) { counter = new counter(); map.put(key, counter); } if (value.equals("1")) { counter.incrementone(); } else { counter.incrementmixed(); } } reader.close(); bufferedwriter writer = new bufferedwriter(new filewriter("output.txt")); for(map.entry<string, counter>entry:map.entryset()){ counter counter = entry.getvalue(); if(counter.one>0){ writer.write(string.valueof(counter.one)); writer.write(" x "); writer.write(entry.getkey()); writer.write(" 1"); writer.newline(); } if(counter.mixed>0){ writer.write(string.valueof(counter.mixed)); writer.write(" x "); writer.write(entry.getkey()); writer.write(" mixed"); writer.newline(); } } writer.close(); }
}
Comments
Post a Comment