0

I am trying to make a calculator with Java, and everything worked normally until this error popped up: imcompatible types:

String cannot be converted to double

Invalid value type 'String' for format specifier '%.0f', parameter 1

This is the script:

private void btnResultActionPerformed(java.awt.event.ActionEvent evt) {                                     
    String answer;

    second = Double.parseDouble(display.getText()); // The second number to count
    if (operation == "+") {
        answer = String.valueOf(first + second);
        result = String.format("%.0f",answer);
        display.setText(String.valueOf(result));
    } else if (operation == "-") {
        answer = String.valueOf(first - second);
        result = String.format("%.0f",answer);
        display.setText(String.valueOf(result));
    } else if (operation == "*") {
        answer = String.valueOf(first * second);
        result = String.format("%.0f",answer);
        display.setText(String.valueOf(result));
    } else if (operation == "/") {
        answer = String.valueOf(first / second);
        result = String.format("%.0f",answer);
        display.setText(String.valueOf(result));
    }

}   

Variables "first", "second" and "result" are doubles, operation is String. I did not have this error before, but after a while it started to error those so I added String.valueOf(first + second); , before it was answer = first + second; but adding valueOf fixed that. Now the problem still exists on "result = String.format("%.0f", answer);"

Timppa
  • 353
  • 2
  • 7
  • 24

3 Answers3

1

You'll want to make answer a double as well, since you're doing calculations. It's only at display/formatting phase where you should convert to a String.

You should put only the calculation part inside the if (i.e. answer = first + second, with answer being a double), and afterwards convert it to a String to avoid duplicate code.

Finally use equals() to compare Strings, so operation.equals("+") and not ==.

Community
  • 1
  • 1
Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • I changed that operation == "+" to operation.equals("+") and it works the same way as == would. I removed answer and added your result = String.format(...); but it still says, that String cannot be converted to double. Might there be an error with my NetBeans? Because I followed a video guide to make this, it works for him, so did for me but until a moment it popped all those errors. – Timppa May 16 '17 at 10:20
  • Read the linked post, do not use `==` to compare Strings. It doesn't matter if it happens to work by chance this time. Anyway, follow the advice in my answer and it should work nicely. – Kayaman May 16 '17 at 10:24
  • Yeah, thank you for that, will remember in future! However, the error exists on result = String.format(...) AND display.setText(result); but I changed this to display.setText(String.valueOf(result)); which I had before and the error disappeared, the only thing that ain't working is the String.format. – Timppa May 16 '17 at 10:39
  • It's still the same basic thing. Do your calculations using `doubles`. Then convert the result to `String` with `String.format()`. – Kayaman May 16 '17 at 11:02
  • answer = first + second; display.setText(String.format("%.0f", answer)); <- This fixed all errors, thank you! =) – Timppa May 16 '17 at 11:07
1

With %.0f as argument for String formatter, you need to pass a number (a float), but in your snippet answer is currently a String.

You can use %s in formatter for a String, or (better) pass as second argument the result of the operation as a float instead of a String

To resume :

String answer = "1";
System.out.println( String.format("%.0f",answer) ); // Throws an exception


double answer = 1.d;
System.out.println( String.format("%.0f",answer) ); // Prints 1
Prim
  • 2,880
  • 2
  • 15
  • 29
  • I changed my answer from string to double right now. The problem still exists. – Timppa May 16 '17 at 10:37
  • @Timppa Please update your post with your current code and error – Prim May 16 '17 at 10:40
  • double answer; second = Double.parseDouble(display.getText()); if (operation.equals("+")) { answer = first + second; result = String.format("%.0f", answer); ruutu.setText(String.valueOf(result)); <- This is the most important part of it, rest of the code are basically the same. And error: String cannot be converted to double for the result = String.format(); line – Timppa May 16 '17 at 10:55
  • Where is the declaration of result ? Are you sure is declared as String ? – Prim May 16 '17 at 11:38
  • Check the correct answer, I fixed it in there by removing one line. – Timppa May 16 '17 at 12:22
  • Yes, sure. So probably i had right : your variable `result` was not declared as a String, and you tried to store a String on it. Impossible for us to confirm, the declaration of result doesn't appear on your post ;) – Prim May 16 '17 at 12:26
1

If result is of double data type

double result = first + second;

display.setText(String.valueOf(result));
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
  • Yeah, that fixed one of the problems but String.format is still popping an error message. – Timppa May 16 '17 at 10:39
  • http://stackoverflow.com/questions/703396/how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0 – Cork Kochi May 16 '17 at 15:09