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 11 of 11

Thread: Help with run time error

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with run time error

    My algorithm method in another class is

    public double getFutureValue(int numberOfYears, int numberOfTimes)
    {
    return principal * Math.pow((1 + interestRate / numberOfTimes), (numberOfYears * numberOfTimes));
    }

    my other class, main class, is supposed to display the year and balance. I created a loop so that the year will display as to how many years the user inputs, but the balance for every year is the same. I don't know what is wrong. Help pleaseee.

    Here is my method in main with the loop:

    public void menu3()
    {
    System.out.println("If you invested " + f.format(principal) + " in a bank account paying "
    + f.format(interestRate) + " for " + numberOfYears + " years, \ncompounded " + numberOfTimes
    + " times per year, then your balance will be:");

    invest.setPrincipal(principal);
    invest.setInterestRate(interestRate);
    System.out.println("Years \tBalance");
    System.out.println("----------------");
    for(int year = 1; year <= numberOfYears; year++)
    {
    System.out.println(year + "\t" + f.format(invest.getFutureValue(numberOfYears, numberOfTimes)));
    }

    }


    It displays (if user inputs numberOfYears to 6 and all other info thats unrelated to my question)
    year balance
    1 $1647.01
    2 $1647.01
    3 ""
    4 ""
    5 ""
    6 ""

    What should I do so that it compounds the value for each time and not just the one year?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with run time error

    What should I do so that it compounds the value for each time an
    Do you have the algorithm for computing the value you need?
    Can you get it from google?
    Given the algorithm, we should be able to show you how to code it in java.

    What should I do so that it compounds the value for each time and not just the one year?
    Not knowing the algorithm, I can only guess. You must change the value passed to the method each time around the loop or it will always return the same result. So you need to change one of the variables used in the computation to get the correct results. It could be the principal or the year (your current code always passes the total number of years vs the current year)

  3. #3
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Help with run time error

    You forgot to pass 'principle' and 'interestRate' to your 'getFutureValue' method. I find it difficult to remember all that to, and I usually just forget about the method (as an object, or class). I would eliminate the method and make this one line change:

    //System.out.println(year + "\t" + f.format(invest.getFutureValue(numberOfYears, numberOfTimes))); <<<original line commented out
    System.out.println(year + "\t" + f.format(principal * Math.pow((1 + interestRate / numberOfTimes), (numberOfYears * numberOfTimes)));

    This also has the advantage of taking a few less CPU cycles to compute. Now if your math is wrong in there that's up to you, I just copied and pasted you algorithm.

    A long term QBasic programmer, this is my 5th day with Java. I messed around with making functions (methods) for algorithms in QB and I found that, for something this simple, it's just easier to do it this way. For learning purposes, and good form, leave the method in and try passing all the needed variables to it.

    As Norm said:

    Quote Originally Posted by Norm View Post
    You must change the value passed to the method each time around the loop or it will always return the same result. So you need to change one of the variables used in the computation to get the correct results. It could be the principal or the year (your current code always passes the total number of years vs the current year)
    You need to pass the variable to the method in order for the method to properly use its value. In this case principle and interestRate
    Last edited by Spidey1980; August 10th, 2011 at 12:04 PM. Reason: grammatical

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help with run time error

    This also has the advantage of taking a few less CPU cycles to compute
    How do you come to this conclusion? And even if I did believe this, saving 'cpu cycles' that only takes a fraction of a fraction of a second - I don't see the point...especially when considering the following:

    I would eliminate the method and make this one line change:
    Me personally, I consider removing the method bad advice. Methods, classes, etc...are there for a reason which goes beyond good practice, and learning bad practice to begin with because its 'easier' is, well, bad practice. My .02

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with run time error

    So even with a loop, I need to pass numberOfYears and numberOfTimes to it every time?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with run time error

    Do they change as the loop goes around? If they stay the same then:
    Are they class variables that the called method can see (the variables are in scope)?
    If the method can see them, then it could use their values from where it sees them.

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with run time error

    Yes it should change. If the user inputs number of years as 10 then it should display values 1-10 in numberOfYears in my algorithm, so:

    algorithm -- principal * Math.pow((1 + interestRate / numberOfTimes), (numberOfYears * numberOfTimes));

    principal * Math.pow((1 + interestRate / numberOfTimes), (1 * numberOfTimes));
    principal * Math.pow((1 + interestRate / numberOfTimes), (2 * numberOfTimes));
    principal * Math.pow((1 + interestRate / numberOfTimes), (3 * numberOfTimes));

    and so on for how many years they input, in this case 10.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with run time error

    If the variables are changing as the loop goes around, then you would need to pass them to the method.
    int var = 1;
    for(int i=0; i < nbrTimesAround; i++ )
       callMethod(var, otherstuff);  // pass var because it changes every time around loop
       var = var + 2*i; // changing the value of var in the loop
    }
    Last edited by Norm; August 11th, 2011 at 11:52 AM.

  9. #9
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with run time error

    The nbrTimesAround would be the same as the variable though. My method to put the parameters into is invest.getFutureValue(numberOfYears, numberOfTimes). So if I wrote it like you said it would have to be

    int numberOfYears = keyboard.nextInt();
    for( int i = 1; i < numberOfYears; i++ )
    {
    System.out.println(year + "\t" + f.format(invest.getFutureValue(numberOfYears, numberOfTimes)));
    numberOfYears = numberOfYears + 2*i;
    }

    But that would keep it going forever

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with run time error

    Copy the value to another variable that is not changed in the loop:
    int numberOfYears = keyboard.nextInt();
    int loopCntrl = numberOfYears; // save in temp loop control variable
    for( int i = 1; i < loopCntrl; i++ ) {
      System.out.println(year + "\t" + f.format(invest.getFutureValue(numberOfYears, numberOfTimes)));
      numberOfYears = numberOfYears + 2*i;  // ????WHAT DOES THIS DO?
    } // end for(i)

  11. The Following User Says Thank You to Norm For This Useful Post:

    white97 (August 11th, 2011)

  12. #11
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with run time error

    I figured it out. Thanks for your help!

Similar Threads

  1. Run Time Error.
    By seymour001 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 10th, 2011, 12:10 PM
  2. [SOLVED] Java run time error: 'java.lang.NullPointerException"
    By ChandanSharma in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 20th, 2011, 11:53 AM
  3. [SOLVED] Run time error
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2011, 11:05 AM
  4. [SOLVED] Experiencing a run time error, don't know what the problem is
    By scooty199 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2010, 10:21 AM
  5. Struts Application, Complies perfectly but error at run time
    By Prarthana in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 18th, 2010, 01:49 AM