AHMED MOKHTAR
AHMED MOKHTAR

Reputation: 3

error in my project java The operator - is undefined for the argument type(s) String, double

System.out.println("the result is: \n" + num1 - num2);

The operator - is undefined for the argument type(s) String, double

Upvotes: 1

Views: 4104

Answers (2)

Mohammad
Mohammad

Reputation: 737

num1 - num2 is an equation that needs to be in brackets , because you need to add the result of it to the string , so the code should be like this

System.out.println("the result is: \n" + (num1 - num2));

Upvotes: 1

tung
tung

Reputation: 769

What you're trying to do here corresponds to

("the result is: \n" + num1) - num2

in which num1 gets cast to a string and added to your first String. Afterwards, you try to subtract an int or something from a String which is not defined.

You can fix this by using proper parentheses settings

"the result is: \n" + (num1 - num2)

So that the subtraction of num1- num2 is done before the Cast to String and the concatenation

Upvotes: 1

Related Questions