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.

Here is my code thus far:

``````import java.util.Scanner;
public class investmentCalculator {

public static void main (String[] args) {

Scanner input = new Scanner(System.in);

double shares, purchase, selling;
int amountGain = 0, percentGain;

//User input for initial investment
System.out.print("Enter the number of shares: ");
double shares1 = input.nextDouble();

//User input for years invested
System.out.print("Enter the purchase price: ");
double purchase1 = input.nextDouble();

//User input for monthly interest rate
System.out.print("Enter the selling price: ");
double selling1 = input.nextDouble();

//Compute the future investment
double percentGain1 = (purchase1 - selling1) / (purchase1);

//Display result
System.out.println("Percent gain / loss:  " + percentGain1);
System.out.println("Amount gain / loss: " + amountGain);
}
}
``````

My Output is not correct though. I am thinking it has to do with my calculations. I am stuck and not sure what else I have to do to tweek it. Would someone be as kind to help me through this program? Many thanks in advance.

Here is my output:

``````Enter the number of shares: 1000
Enter the purchase price: 10.00
Enter the selling price: 20.00
Percent gain / loss:  -1.0
Amount gain / loss: 0
``````

this is not really a Java question, it's your logic that doesn't make much sense.

percent gain or loss, against what? what you originally paid?
(shares * purchase1) = originalCost
next, what you earned:
(shares * selling1) = earning

earning - originalCost => amountGainedOrLost
percentGain = amountGainedOrLost/(originalCost/100)

// that is if I understand your requirements correctly.

That is correct stultuske

Ok, I think I have the right understanding here is what I have thus far:

``````import java.util.Scanner;

public class investmentCalculator {
public static void main (String[] args) {

Scanner input = new Scanner(System.in);

//User input for initial investment
System.out.print("Enter the number of shares: ");
double shares = input.nextDouble();

//User input for years invested
System.out.print("Enter the purchase price: ");
double purchase = input.nextDouble();

//User input for monthly interest rate
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/100);

int earning = 0;
int original = 0;

System.out.println("Percent gain / loss: " + amountGainedOrLost + "%");
System.out.println("Amount gain / loss: \$" + percentGain);

}
}
``````

And my output is this.

``````Enter the number of shares: 1000
Enter the purchase price: 10.00
Enter the selling price: 20.00
Percent gain / loss: 10000.0%
Amount gain / loss: \$100.0
``````

I think I have to many zero's. Any suggestion's on how to fix this? Thank you in advance.

your first mistake is that you've switched the percentGain and amountGainedOrLost variables in your print statements. the amountGained returs correct information, so no need to revise that.

as for percentGain: I've re-checked it (as I said, didn't really know what you needed), but try and replace your last lines by this:

``````double percentGain = 100*((selling-purchase)/purchase);
System.out.println("Percent gain / loss: " + percentGain + "%");
System.out.println("Amount gain / loss: \$" + amountGainedOrLost);
``````

since the number of stock for both purchase and selling remain the same, you can calculate the percentage by just the price.
as for the

``````int earning = 0;
int original = 0;
``````

since you don't use them, you may just as well remove these lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.