# Thread: The difference from one equation is being added to the total of another

1. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## The difference from one equation is being added to the total of another

I am writing a program for finding the profit made after paying the broker. here is my code, I am wondering why
Java Code:
`paidBroker = commission + commissionAfterSale;`
Java Code:
`noProfit = profit - paidBroker;`
.

Here is my full program, you can find these lines of code on lines 64 and 70.

Java Code:
```import java.util.Scanner;
public class StockTransaction {

public static void main(String[] args) {
// TODO Auto-generated method stub

//Variable Declaration Section
double amceStockPurchasePrice;    //Price per Acme Stock
int acmeStockPurchased;           //Number of Acme Stock purchased
double commissionRate;            //Commission of total price paid to broker
double acmeStockSoldPrice;        //Price per Acme Stock sold
double grossTotalPaid;            //Gross Total paid for Acme Stock
double commission;                //Price paid to broker
double profit;                    //Profit made from selling stock
double commissionAfterSale;       //Price paid to broker after the Joe sold his Acme Stock
double paidBroker;                //Amount Joe paid his broker in total
double noProfit;                  //Total Amount of money Joe has after he paid his broker

//Variable Initialization Section

//Keyboard
Scanner keyboard = new Scanner(System.in);

//Code Section + Output Section
System.out.print("How much was each Acme Share? \$");
amceStockPurchasePrice = keyboard.nextDouble();

System.out.print("How many shares did Joe purchase? ");
acmeStockPurchased = keyboard.nextInt();

//Calculate Gross Total Paid
grossTotalPaid = acmeStockPurchased * amceStockPurchasePrice;

System.out.print("Joe paid \$" + grossTotalPaid + " for " + acmeStockPurchased + " shares. ");

System.out.print("\nWhat percentage was Joe's brokers commission? ");
commissionRate = keyboard.nextDouble();

//Calculate Commission after Joe bought the stock
commission = grossTotalPaid * commissionRate;

System.out.print("Joe's broker was paid \$" + commission);

System.out.print("\nJoe decided to sell his stock, how much did he sell each share for? \$");
acmeStockSoldPrice = keyboard.nextDouble();

System.out.print("How many shares did Joe sell? ");
acmeStockPurchased = keyboard.nextInt();

//Calculate Gross Total Received after sales

System.out.print("Joe recieved \$" + grossTotalReceived + " for selling all " + acmeStockPurchased + " shares.");

//Calculate Commission after Joe sold the stock

System.out.println("\nJoes broker was paid \$" + commissionAfterSale + " after the sale.");

//Calculate Profit Joe made from selling his stock
profit = grossTotalReceived - grossTotalPaid - commission - commissionAfterSale;

//Calculate Amount Spent by Joe from paying his broker
paidBroker = commission + commissionAfterSale;

System.out.print("Joe's broker was in total paid " + paidBroker);

//Calculate total after Joe paid his broker
noProfit = profit - paidBroker;

if(profit > paidBroker)
System.out.print("\nJoe recieved \$" + profit + " in profit.");
else if(profit == paidBroker)
else
System.out.println("\nJoe lost \$" + noProfit);

}

}```

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The difference from one equation is being added to the total of another

Look at lines 62, 65, and 70.

Regards,
Jim

3. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## Re: The difference from one equation is being added to the total of another

Originally Posted by jim829
Look at lines 62, 65, and 70.

Regards,
Jim
I do not see how this adds noProfit to paidBroker though. Can you explain this to me, please.

Regards,
Sithclone3

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The difference from one equation is being added to the total of another

I thought you said that paidBroker is added to noProfit (first post). In any event, please provide the input values and the output values and point out what the output should actually be.

Thanks,
Jim

5. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## Re: The difference from one equation is being added to the total of another

This is my output:

How much was each Acme Share? \$32.87

How many shares did Joe purchase? 1000

Joe paid \$32870.0 for 1000 shares.

What percentage was Joe's brokers commission? .02

Joe's broker was paid \$657.4

Joe decided to sell his stock, how much did he sell each share for? \$33.92

How many shares did Joe sell? 1000

Joe recieved \$33920.0 for selling all 1000 shares.

Joes broker was paid \$678.4 after the sale.

Joe's broker was in total paid 1335.8

Joe lost \$-1621.6

Joe's profit is \$1050, he paid his broker \$1335.8, this means he lost \$285.8. Instead its added the 285.5 to the 1335.8 which gives me 1621.6.
Last edited by sithclone3; 01-21-2015 at 09:50 PM.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
15

## Re: The difference from one equation is being added to the total of another

In line 62 you subtract the broker's fees to get the net profit. But in line 70 you subtract them out yet again. I haven't tested this, but that looks like a problem.

Regards,
Jim

7. Member
Join Date
Jun 2014
Posts
89
Rep Power
0

## Re: The difference from one equation is being added to the total of another

Originally Posted by jim829
In line 62 you subtract the broker's fees to get the net profit. But in line 70 you subtract them out yet again. I haven't tested this, but that looks like a problem.

Regards,
Jim
I fixed it. I got rid of noProfit, that seemed to the issue.

8. ## Re: The difference from one equation is being added to the total of another

Originally Posted by sithclone3
I fixed it. I got rid of noProfit, that seemed to the issue.
No profit sucks anyway :)

9. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

## Re: The difference from one equation is being added to the total of another

Originally Posted by sithclone3
I fixed it. I got rid of noProfit, that seemed to the issue.
Read that quote in SurfMan's signature.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•