0

I am making a restaurant ordering system menu for homework in Java. I am to make a receipt in the form of the program creating a text file with the table contents in it. I am however having trouble doing this. All my table contents are strings.

This is the code for exporting the contents of the table:

try{       
   BufferedWriter bfw = new BufferedWriter(new FileWriter("Data.txt"));
   for(int i = 0 ; i < tableSalesFood.getColumnCount() ; i++){
       bfw.write(tableSalesFood.getColumnName(i));
       bfw.write("\t");
   }

   for (int i = 0 ; i < tableSalesFood.getRowCount(); i++){
       bfw.newLine();
       for(int j = 0 ; j < tableSalesFood.getColumnCount();j++){
          bfw.write((String)(tableSalesFood.getValueAt(i,j)));
          bfw.write("\t");;
       }
   }

   bfw.close();   
   }catch(Exception ex){
      ex.printStackTrace();
   }

The program returns the exception error when I click the button:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
TimeToCode
  • 901
  • 2
  • 16
  • 34
stereoputrid
  • 734
  • 1
  • 5
  • 10
  • 4
    `tableSalesFood.getValueAt(i,j).toString()` getValueAt returns an Object, not a String. The Object may be a String, an Integer, a Double, or a RubberDucky. Do not cast to String, invoke the toString method. – Compass Nov 02 '16 at 13:42
  • For serialization / deserialization to/from text, It is better to use the underlying `TableModel` to/from text, rather than the JTable itself. – ControlAltDel Nov 02 '16 at 13:55

2 Answers2

3

Apparently it's caused by (String)(tableSalesFood.getValueAt(i,j), you're trying to cast Integer to a String. Please make sure you know what a ClassCastException is, see for instance this question.

You can fix the error by conversion, not casting:

Objects.toString(tableSalesFood.getValueAt(i,j), "");

Class Objects is defined in java.util package.

Community
  • 1
  • 1
borowis
  • 1,207
  • 10
  • 17
  • I did this, but it still gives me an error : java.lang.NullPointerException – stereoputrid Nov 03 '16 at 06:10
  • are you using `Objects.toString`? Which line NullPointerException refers to? – borowis Nov 03 '16 at 10:05
  • yes, this is what I used: Objects.toString(tableSalesFood.getValueAt(i,j), ""); – stereoputrid Nov 03 '16 at 13:23
  • which line does NullPointerException refers to? that one with Objects.toString? or another line? – borowis Nov 03 '16 at 13:27
  • it already works. sorry and thank you. however, the only data in the txt file are the column names. – stereoputrid Nov 03 '16 at 13:45
  • do you get blank lines, or just column names and that's all? – borowis Nov 03 '16 at 16:01
  • just column names. – stereoputrid Nov 03 '16 at 16:13
  • so, your row count is 0, otherwise you would have writen blank lines to the file (or your text editor doesn't show whitespaces). I also think that something changed from yesterday, because if rowCount were 0 yesterday there wouldn't be any classcast exception. so first, please make sure that the rowCount is indeed 0 (just print it to console), and if it is -- you've got to deal with some other error – borowis Nov 03 '16 at 16:19
  • 1
    yep. rowCount s 0. – stereoputrid Nov 03 '16 at 16:35
  • so, that's completely different story. I would suggest to ask a new question on why it's zero. because there might be a number of reasons why it is (first that comes to mind is row filtering, see this for instance http://stackoverflow.com/questions/23626951/jtable-row-count-vs-model-row-count). after this problem is solved, the code that writes values to the file should work just fine – borowis Nov 03 '16 at 20:00
1

You can actually create tab-separated values by using JTable’s default TransferHandler to export the JTable’s headers and values as a String:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
table.getTransferHandler().exportToClipboard(table, clipboard,
    TransferHandler.COPY);

try {
    String text = (String) clipboard.getData(DataFlavor.stringFlavor);
    Files.write(Paths.get("Data.txt"), Collections.singleton(text),
        Charset.defaultCharset());
} catch (UnsupportedFlavorException | IOException e) {
    throw new RuntimeException(e);
}
VGR
  • 40,506
  • 4
  • 48
  • 63
  • I tried this but it the Data.txt file doesn't contain the contents of the table. It contains this:Toolkit toolkit = Toolkit.getDefaultToolkit(); Clipboard clipboard = toolkit.getSystemClipboard(); table.getTransferHandler().exportToClipboard(table, clipboard, TransferHandler.COPY); try { String text = (String) clipboard.getData(DataFlavor.stringFlavor); Files.write(Paths.get("Data.txt"), Collections.singleton(text), Charset.defaultCharset()); } catch (UnsupportedFlavorException | IOException e) { throw new RuntimeException(e); } – stereoputrid Nov 03 '16 at 06:23