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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Investment Calculator

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Investment Calculator

    Can you explain what values in the print out are wrong and add some comments to show what they should be?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: November 12th, 2013, 04:57 AM
  2. Calculator
    By Vibex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2012, 05:23 PM
  3. investment interest display problem
    By df75douglas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 4th, 2011, 07:32 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM

Tags for this Thread