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

1
  • Also consider changing ` Double getRate()` to ` double getRate()` Commented Apr 1, 2017 at 8:53

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

16 Comments

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?
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.
If my explanation is not clear, feel free to ask more questions.
Do you have a hardware store sample program?
Nope sorry, I don't have such sample application. If something is still not clear, ask more questions.
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.