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: My program always gives me an output of 0.0, what's wrong with it?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation My program always gives me an output of 0.0, what's wrong with it?

    *Edit: I fixed it thanks to aprabhat! Thanks for your help!*

    I'm at the beginner level when it comes to Java, so I don't really know much about programming, but I'm taking a class on Java and we were asked to write a program that prompts the user for three different values (P,i, and n as you will see in my code) and use those values to calculate interest on a certain amount of money (P) that the user is prompted for. So I wrote it all and it will compile and run, but for some reason I always get an output of zero. I have tried using both double and float variables instead of integer variables because I know that integer variables always round, but it doesn't work. Also, when I use float variables my program won't compile because the power method requires a double variable because there is "a possible loss of precision" so I used double variables in the code in this post. Thanks ahead of time for the help. Well anyways, here's my code:


    public class Unit7Assignment_2
    {
    public static void main( String [] args )
    {
    int P = 0;
    double i = 0;
    int n = 0;
    double S = interest( P, i, n );

    P = Input.getInt( "Please enter an integer value for the amount of money deposited at the beginning of the year" );
    i = Input.getDouble( "Please input a double value for the annual interest rate" );
    n = Input.getInt( "Please enter an integer value for the amount of time in years that the money deposited has accrued the interest" );

    if ( n == 1 )
    {
    System.out.println();
    System.out.println( "The amount of money in the account after 1 year is $" + S );
    System.out.println();
    }
    else
    {
    System.out.println();
    System.out.println( "The amount of money in the account after " + n + " years is $" + S );
    System.out.println();
    }
    }

    public static double interest( double P, double i, double n )
    {
    double j = 1 + i;
    double k = Math.pow( j, n );
    double m = k * P;
    return m;
    }
    }
    Last edited by thepiranha; December 31st, 2013 at 01:32 AM. Reason: Fixed the Problem!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My program always gives me an output of 0.0, what's wrong with it?

    Check the order of your commands. Java executes top-down in a block.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My program always gives me an output of 0.0, what's wrong with it?

    Which commands? The equations to calculate my return value?

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: My program always gives me an output of 0.0, what's wrong with it?

    In your code
    double S = interest( P, i, n );
    P = Input.getInt( "Please enter an integer value for the amount of money deposited at the beginning of the year" );
    i = Input.getDouble( "Please input a double value for the annual interest rate" );
    n = Input.getInt( "Please enter an integer value for the amount of time in years that the money deposited has accrued the interest" );
    you take input for the variables P,i,n after calling the function so the function take value 0 as all the arguments and return 0.0 as result.

  5. The Following User Says Thank You to aprabhat For This Useful Post:

    thepiranha (December 31st, 2013)

Similar Threads

  1. Output is Wrong .How to fix it?
    By cmen535 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 5th, 2013, 01:15 AM
  2. My output is in the wrong order! (if-else) PLEASE HELP
    By Carecinos in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 26th, 2013, 09:38 AM
  3. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 15th, 2013, 08:35 PM
  4. My loop compiles and seems correct, but gives wrong output.
    By new2.java in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2013, 10:31 PM
  5. Sudoku: wrong output?
    By lisa92 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: April 15th, 2012, 12:10 PM