Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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.

```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.

```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.

2. ## Re: Investment Calculator

Can you explain what values in the print out are wrong and add some comments to show what they should be?

3. ## Re: Investment Calculator

Yes, absolutely.

My output should look something like this...

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

But instead it looks like this:

```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.

4. ## 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