1

txtRate.setText(emp.getRate());

I cant display the data from the csv file in the textbox because it keeps on saying

double cannot be converted to java.lang.String. My method is public Double getRate() If i try to make is public String it would work but i cant use the value for calculation because it already has been set to String

Rubberbox
  • 3
  • 3

2 Answers2

1

The class you are working on contains method with signature setText(String value) but there's no method with signature setText(Double value).

When you call a method compiler checks the parameter type and tries to recognize what method should be in use for this case.

As you are using Double type for the parameter, the compiler is unable to find a method with signature setText(Double value) and you get the error you have reported in your question.

To overcome this issue, convert your parameter from Double to String type before passing it to setText method.

txtRate.setText(String.valueOf(emp.getRate()));

or

txtRate.setText(emp.getRate().toString());

But the second approach may throw NullPointerException if emp.getRate() returns null, so the first approach is safer.

In case you want to format your numbers in a fancy way, consider this tutorial: https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

  • I just want it to remain double not convert to String. Because i need it for calculations. I cant add up String and an integer. How can i make it remain as Double? – Rubberbox Apr 01 '17 at 08:56
  • You convert your Double to String only to put it to the text box. Your getRate() method returns Double and you can use it for calculations as well. –  Apr 01 '17 at 08:57
  • If my explanation is not clear, feel free to ask more questions. –  Apr 01 '17 at 08:59
  • Do you have a hardware store sample program? – Rubberbox Apr 01 '17 at 09:08
  • Nope sorry, I don't have such sample application. If something is still not clear, ask more questions. –  Apr 01 '17 at 09:10
  • So i have a csv file. And i need to update a certain cell with a new number. How can i do it? – Rubberbox Apr 01 '17 at 09:57
  • If you are working with a text (csv) file, I recommend to consider [RandomAccessFile](https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html) java class. CSV file consists of lines of data, so you can read a line, find a cell offset in this line, update the value of the cell, and [replace the updated string](http://stackoverflow.com/questions/23466179/java-replace-specific-string-in-textfile) in your csv file –  Apr 01 '17 at 10:11
  • Also, if you are working with MS Excel files, consider [Apache POI framework](https://poi.apache.org/spreadsheet/quick-guide.html) –  Apr 01 '17 at 10:15
  • Yes. I searched and most is RAF is the most recomended – Rubberbox Apr 01 '17 at 10:46
  • That's because you need to navigate and change an arbitrary part of a file. If your csv file is a large one and you have many cells to update I would recommend the following approach: create a new csv (text) file, read line by line your source csv file, update these lines and put them to the new file. When you have finished, delete source csv file, and rename your new csv file to have the same name as had your source file before. –  Apr 01 '17 at 10:58
  • Do you know why – Rubberbox Apr 02 '17 at 09:27
  • I did not understand your question. Why what? Please elaborate your question. –  Apr 02 '17 at 09:29
  • I have another problem. – Rubberbox Apr 02 '17 at 10:54
  • java.lang.NumberFormatException: – Rubberbox Apr 02 '17 at 10:54
  • For input string: "Rate" (in sun.misc.FloatingDecimal) – Rubberbox Apr 02 '17 at 10:55
  • Looks like your string, that you are trying to convert to number contains delimiters or other formatting, that is not expected by your formatter when you call `parse` method. Make sure you have correct formatting pattern set for your formatter according to your source string. –  Apr 02 '17 at 11:20
0

Displaying data on the textbox require variable to be string, But calculation require variable to be double the answer is when displaying cast to string and when calculation don't

Fady Saad
  • 1,133
  • 7
  • 13