r/javahelp • • Jan 30 '13

Help please!

I keep getting an error stating "error: bad operand types for binary operator '-' " I don't know how to fix it. Here is my code:

/* Programmer name: Corey Kapanka Project name: Course, day and time: CIS1500, Wednesday 11:00AM Date of completion: 1/29/13 Purpose: Prints amount paid for stock, amount stock sold for, amount paid to stock broker when bought and sold, and the profit made*/

public class Stock { public static void main(String[] args) { int AmtStock; //Amount of Stock bought double PriceBought=32.87; double PriceSold=33.92; double AmtBroker=.02;

AmtStock=1000;

System.out.println("Joe paid $"+ AmtStock * PriceBought +" for the shares of stock.");
System.out.println("Joe sold his stock for $"+ AmtStock * PriceSold +".");
System.out.println("When Joe bought his stock he paid his broker $"+ AmtBroker * (AmtStock * PriceBought) +".");
System.out.println("When Joe sold his stock he paid his broker $"+ AmtBroker * (AmtStock * PriceSold) +".");
System.out.print("Joe made $"+ ((AmtStock * PriceSold) + (AmtBroker * (AmtStock * PriceSold)))-((AmtStock * PriceBought) + (AmtBroker * (AmtStock * PriceBought)))+".");
} //end method main

} //end class Stock

1 Upvotes

3 comments sorted by

1

u/kcoPkcoP Medium Noob Brewer Jan 30 '13

My first explanation was wrong, so I deleted it.

Your problem is

System.out.print("Joe made $"+ ((AmtStock * PriceSold) + (AmtBroker * (AmtStock * PriceSold)))-((AmtStock * PriceBought) + (AmtBroker * (AmtStock * PriceBought)))+".");

that you need to enclose the stuff being subtracted in parenthesis so that it will be evaluated as a mathematical expression rather than as operator on Strings. It's probably simplest to add a double profit and do the whole calculation there.

1

u/kcoPkcoP Medium Noob Brewer Jan 30 '13

Here's the behavior for the normal arithmetic operators and as you can see + and - both operate unexpectedly inside a println() if there's string operations prior to the arithmetic, whereas *, /, % all behave normally regardless. My guess is that that is because *,/,% have higher priority than string operations and are performed before they come into contact with strings and that it therefore doesn't matter that they're undefined. Whereas the - operates on the same priority level as strings and can give an error.

double a = 1.0;
double b = 2.0;
System.out.println(a+b);		 outputs: 3.0
System.out.println("" + a+b);		 outputs: 1.02.0  // not the same behavior as when a String isn't present
System.out.println(a-b);		 outputs: -1.0
System.out.println("" + a-b);		 outputs: error 
System.out.println(a*b);		 outputs: 2.0
System.out.println("" + a*b);		 outputs: 2.0
System.out.println(a/b);		 outputs: 0.5
System.out.println("" + a/b);		 outputs: 0.5
System.out.println(a%b);		 outputs: 1.0
System.out.println("" + a%b);		 outputs: 1.0

1

u/TheHorribleTruth Kind of meh Jan 30 '13

This will help

Also, please read the sidebar before posting.

All post titles must be descriptive.