swing - JAVA read text files, count numbers and write it to Jtable -
i still learning java , have been trying find solution program few days, haven't gotten fixed yet.
i have many text files (my program saves). files this:
text (tab) number (tab) number (tab)... text (tab) number (tab) number (tab)...
(tab) means there tabulation mark, text means text (string), number means there number (integer).
number of files can 1 32 , file names like: january1; january2; january3...
i need read of files (ignore strings) , sum numbers so:
while ((line = br.readline()) != null) { counter=counter+1; string[] info = line.split("\\s+"); for(int j = 2; j < 8; j++) { int num = integer.parseint(info[j]); data[j][counter]=data[j][counter]+num; } };
simply want sum "tables" array of arrays (or similar kind of variable) , display table. if knows solution or can link similar calculation, awesome!
so, see it, have 4 questions need answered, goes against site etiquette of asking question, give shot
- how list series of files, presumably using kind of filter
- how read file , process data in meaningful way
- how manage data in data structure
- show data in
jtable
.
listing files
probably simplest way list files use file#list
, pass filefilter
meets needs
file[] files = new file(".").listfiles(new filefilter() { @override public boolean accept(file pathname) { return pathname.getname().tolowercase().startswith("janurary"); } });
now, i'd write method took file
object representing directory want list , filefilter
use search it...
public file[] listfiles(file dir, filefilter filter) throws ioexception { if (dir.exists()) { if (dir.isdirectory()) { return dir.listfiles(filter); } else { throw new ioexception(dir + " not valid directory"); } } else { throw new ioexception(dir + " not exist"); } }
this way search number of different set of files based on different filefilter
s.
of course, use newer paths/files
api find files well
reading files...
reading multiple files comes down same thing, reading single file...
// bufferedreader has nice readline method makes // easier read text with. use scanner // prefer bufferedreader, that's me... try (bufferedreader br = new bufferedreader(new filereader(new file("...")))) { string line = null; // read each line while ((line = br.readline()) != null) { // split line individual parts, on <tab> character string parts[] = line.split("\t"); int sum = 0; // staring first number, sum line... (int index = 1; index < parts.length; index++) { sum += integer.parseint(parts[index].trim()); } // store key/value pairs how } }
now, need way store results of calculations...
have @ basic i/o more details
managing data
now, there number of ways this, since amount of data variable, want data structure can grow dynamically.
my first thought use map
, assumes want combining rows same name, otherwise should list
within list
, outer list
represents rows , inner
list represents column values...
map<string, integer> data = new hashmap<>(25); file[] files = listfiles(somedir, januraryfilter); (file file : files { readfile(file, data); }
where readfile
code before
protected void readdata(file file, map<string, integer> data) throws ioexception { try (bufferedreader br = new bufferedreader(new filereader(file))) { string line = null; // read each line while ((line = br.readline()) != null) { //... // store key/value pairs how string name = parts[0]; if (data.containskey(name)) { int previous = data.get(name); sum += previous; } data.put(name, sum); } } }
have @ collections trail more details
showing data
and finally, need show data. use defaulttablemodel
, have data in structure, why not re-use custom tablemodel
public class summarytablemodel extends abstracttablemodel { private map<string, integer> data; private list<string> keymap; public summarytablemodel(map<string, integer> data) { this.data = new hashmap<>(data); keymap = new arraylist<>(data.keyset()); } @override public int getrowcount() { return data.size(); } @override public int getcolumncount() { return 2; } @override public class<?> getcolumnclass(int columnindex) { class type = object.class; switch (columnindex) { case 0: type = string.class; break; case 1: type = integer.class; break; } return type; } @override public object getvalueat(int rowindex, int columnindex) { object value = null; switch (columnindex) { case 0: value = keymap.get(rowindex); break; case 1: string key = keymap.get(rowindex); value = data.get(key); break; } return value; } }
then apply jtable
...
add(new jscrollpane(new jtable(new summarytablemodel(data)));
take @ how use tables more details
conclusion
there lot of assumptions have made missing context of question; order of files matter? care duplicate entries?
so becomes near impossible provide single "answer" solve of problems
Comments
Post a Comment