• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

bad operand types for binary operator '-'  RSS feed

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to subtract two integers in Java. I am using NetBeans 8.1. I am first reading the integers from text field. Then converting them into integer. Then I am subtracting them But I am getting error:

     
bad operand types for binary operator '-'
first type: String
second type: int



I have written the following code:



Somebody please guide me how to solve this problem.

Zulfi.
 
Ranch Hand
Posts: 155
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
easy - try to build a string to set a numeric value in a method that require String

what happens:

"" + int + int

Java evaluates from left to right - so the first "" is a String - String + int becomes a String - then you try the arithmetic String + int - wich doesn't work.

This is an example, how you should do this:



As it looks like you're a beginner - don't even think about this crap ""+something to get a String - look in the API doc for correct methods how to create a String the right way.
 
Saloon Keeper
Posts: 10136
214
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matt Wong wrote:Java evaluates from left to right - so the first "" is a String - String + int becomes a String


The reason for this is operator associativity, not direction of evaluation.

+ and - have the same precedence, so then associativity kicks in, which for + and - is left-to-right.
 
Marshal
Posts: 64166
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but you are not allowed to apply the − operator to Strings. That is why you are getting the compile‑time error.
 
Zulfi Khan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks all for your responses.

Actually the same thing works for Multiply and divide. If its the question of "String conversion" then why its not doing on Multiply & divide.


Is this a specialized behavior towards "subtract" operator?

Zulfi.
 
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Remember that Stephan said:

Stephan van Hulst wrote:+ and - have the same precedence, so then associativity kicks in, which for + and - is left-to-right.



But + and * do not have the same precedence. In fact * has higher precedence so it's evaluated before +, with the result as you observe in your example.
 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, side question, Zulfi: why are you coding a different method for each math operation?
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And all this business of operator precedence aside, interesting as it may be, it's better to use code like what Matt Wong posted earlier in the thread rather than using "" + number to make a String representation of a number.
 
Stephan van Hulst
Saloon Keeper
Posts: 10136
214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:In fact * has higher precedence so it's evaluated before +


Precedence doesn't affect order of evaluation. Evaluation is always performed as a left-to-right depth-first iteration over the expression tree. Operator precedence determines how the expression tree is built up.
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Precedence doesn't affect order of evaluation. Evaluation is always performed as a left-to-right depth-first iteration over the expression tree. Operator precedence determines how the expression tree is built up.



I guess that's one way of looking at it. But given the expressions a + b + c and a + b * c it's a fact that a + b is evaluated first in the first expression and b * c is evaluated first in the second expression. There's a lot of people who would identify that difference as being a difference in the order of evaluation.
 
Stephan van Hulst
Saloon Keeper
Posts: 10136
214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(b * c) finishes evaluating first, but not before (a + (b * c)) starts evaluating.

I think it's important to be pedantic on this subject, in light of the many questions we get about why (x + ++x) yields 3 and not 4 when x starts out as 1.
 
Zulfi Khan
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for providing solution to this problem.

Operator precedence seems to be right a answer. However the truth is that I did not grasp the other answer.

Believe me I realized this answer sometime back but I could not post my clarification on this.

Sorry, side question, Zulfi: why are you coding a different method for each math operation?
Okay i would try it using switch-case but in case of event driven programming, handler function is a easy option.

Good team work.

God bless you guys.

Zulfi.
 
Paul Clapham
Sheriff
Posts: 24374
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Zulfi Khan wrote:Sorry, side question, Zulfi: why are you coding a different method for each math operation?
Okay i would try it using switch-case but in case of event driven programming, handler function is a easy option.



Yes, I agree with that. If you see old Swing code you will quite often see a single handler function with switch-case code in it, but after a few years people realized that having a different handler for each event was a much better design. Each method (including event handlers) should be designed to do only one thing, and that's exactly what you have done there.

(Okay, it looks like Netbeans made that decision on your behalf but you have still explained the decision correctly.)
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!