Writing data to the java DefaultTableModel
Hello all,
I am attempting to write a program that will read the characters of a text file one by one and keep the count of the number of times each character appears. I would like to use the table data structure "DefaultTableModel"
I would like the table to appear like this
Character/ Frequency
a 10
b 5
c 7
.
.
.
However I am not sure how to use the DefaultTableMode.setColumnIdentifiers method since the identifiers can incerease as the file is read. Here is some of my code
Code :
public DefaultTableModel readFile(File filename) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(filename));
DefaultTableModel myDefaultTableModel = new DefaultTableModel(1,1);
String line = null;
int i = 0;
//Determine
while(reader.ready())
{
line = reader.readLine();
readString(line, myDefaultTableModel, i);
i++;
}
//Close reader object
reader.close();
return myDefaultTableModel;
}
private void readString(String lineString, DefaultTableModel myDefaultTableModel, int i)
{
Object testObject;
StringTokenizer tokenizer = new StringTokenizer(lineString);
String token = tokenizer.nextToken();
for (int stringCount = 0; stringCount < token.length(); stringCount++)
{
myDefaultTableModel.setValueAt(token.charAt(stringCount), 0, 0);
testObject = myDefaultTableModel.getValueAt(0, 0);
}
}
Re: Writing data to the java DefaultTableModel
It might be easier to create a temporary data holder, then create the DefaultTableModel based upon that. One way to do this would be to create a HashMap (keyed with your String and valued with an integer representing the count). Fill up this HashMap as you read the file...then create the TableModel from the map by iterating over its keys and values, filling up the TableModel as you go. Just a suggestion.