# Investment Calculator

• January 24th, 2014, 03:10 PM
lynn66
Investment Calculator
I think I have a cluster of a mess here and was wondering if someone could help me in the right direction. I am trying to write a small program that will calculate the gain and/or loss of the sale of stock. The program will ask the user for the number of shares, the purchase price and the selling price. I am pretty sure that the errors is coming from my calculations in the program.

Code :

```import java.util.Scanner;   public class investmentCalculator { public static void main (String[] args) {   Scanner input = new Scanner(System.in);   //User input for number of shares System.out.print("Enter the number of shares: "); double shares = input.nextDouble();   //User input for purchase price System.out.print("Enter the purchase price: "); double purchase = input.nextDouble();   //User input for selling price System.out.print("Enter the selling price: "); double selling = input.nextDouble();   //Compute double originalCost = (shares * purchase); double earned = (shares * selling); double amountGainedOrLost = (earned - originalCost); double percentGain = amountGainedOrLost/(originalCost/1000);     int earning = 0; int original = 0;     System.out.println("Percent gain / loss: " + amountGainedOrLost + "%"); System.out.println("Amount gain / loss: \$" + percentGain);   } }```

From the looks of my output, I am thinking I have zero '0' problems.

Code :

```Enter the number of shares: 5000 Enter the purchase price: 400.00 Enter the selling price: 450.00 Percent gain / loss: 250000.0% Amount gain / loss: \$125.0```

Would someone please help me with this program. It seems like I have been working on it all day.
• January 24th, 2014, 03:27 PM
Norm
Re: Investment Calculator
Can you explain what values in the print out are wrong and add some comments to show what they should be?
• January 24th, 2014, 03:38 PM
lynn66
Re: Investment Calculator
Yes, absolutely.

My output should look something like this...

Code :

```Percent gain / loss: 250.0% Amount gain / loss: \$125,000.00```

But instead it looks like this:

Code :

```Percent gain / loss: 250000.0% Amount gain / loss: \$125.0```

--- Update ---

I have some issues of zero's not being in the right place. Clearly.
• January 24th, 2014, 03:50 PM
Norm
Re: Investment Calculator
You need to look at each of the equations that compute the values of the variables that have the wrong values.
First do the math for each computation on a piece of paper.
Then add a println statement after EVERY statement that computes a value that prints out the value of the results.
Compare the numbers that were computed manually and written on paper with those that were printed by the program. That will show you which statement in the program is doing it incorrectly.

If you have a problem with understanding and fixing a statement, post the statement and the values that it used and the results that it computed along with what you want the results to be.

For example:
The statement: var = 3/4;
The results: var = 0
Desired results: 0.75