I am currently trying to write a finance program for one of my projects at college in Java. One of the project requirements consists of exporting my JTable data to a text file. Here is my code (Assume that the table is already populated).
When trying to write to the file, however, I get this error:
java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
Any ideas of what I have done wrong...
I originally followed this guide: http://1bestcsharp.blogspot.co.uk/2015/04/java-io-how-to-export-jtable-data-to-txt-File-In-Java.html
but used a different method for writing to the file (https://www.caveofprogramming.com/java/java-file-reading-and-writing-files-in-java.html)
Thanks,
Ben.
try {
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fileWriter);
System.out.println(tbl_Finance.getRowCount() + " " + tbl_Finance.getColumnCount());
for(int i = 0; i < tbl_Finance.getRowCount(); i++) {
for(int j = 0; i < tbl_Finance.getColumnCount(); j++) {
bw.write(tbl_Finance.getValueAt(i, j) + " ");
}
bw.write("\n_________\n");
}
bw.close();
JOptionPane.showMessageDialog(null, "Data Exported To " + fileName);
} catch(Exception ex) {
ex.printStackTrace();
}