Results 1 to 10 of 10
  1. #1
    welsh_rocker is offline Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    7
    Rep Power
    0

    Default While Loop Problem with Java Assignment Program

    Hey guys as part of assignment I have been asked to build a Java Programme that will calculate the number of Years it takes to pay off a Student Loan, I have got the Calculations within the Program right but seem to be having problems with the While Loop it will run, but only go round 1 or maybe 2 times could you look at my code for me and let me know if you spot any problems that I may be missing that is stopping the Loop Implementing Properly.

    Java Code:
    public class LoanPayments {
        // place constant declarations (static final) below.
        // e.g. 3% interest, 9% repayment, 5% pay rise and 10k theshold
            static final double INTEREST = 0.03, REPAID = 0.09, PAYRISE = 0.05, NET = 10000;  
        public static void main(String args[]) {
            double loans, prevloan, newloan, wages, net, interest, newlinterest=0, linterest=0, wagest=0, newwages=0, repaid=0, balance=0, newbalance=0, yrbal=0, year=1;
            // place all variable declarations here
            
            System.out.println(" Student Loan" );
            loans = UserInput.readDouble();
            // prompt and read the loan amount
    
            System.out.println (" Wages ");
            wages = UserInput.readDouble();
            
                interest =  loans * INTEREST;
                // add 3% to the loan
                net = wages - NET;
                // deduct 10k from the wages
                repaid = net * REPAID;
                // from the remaining wages calculate 9% repayment
                linterest = loans + interest;
                balance = linterest - repaid;
                // reapaid loan amount
                wagest = net;
                yrbal= balance * year;
                // sum up repayments and years so far
                System.out.println(" Year " + (int)year);
                MoneyOut.println(" Old Loan ", (int)loans);
                // produce output: current year, old loan, 
                MoneyOut.println(" Loans plus 3% interest ", linterest);
                // loan plus 3% interest, wages, 
                MoneyOut.println(" Wages ", (int)wages);
                MoneyOut.println(" Wages minus threshold ", wagest);
                //wages minus 10,000 pounds, repayment, new loan
                MoneyOut.println(" Repayment ", (int)repaid);
                MoneyOut.println(" New loan amount ", balance);
                
            while (newbalance>0) {
                // while loop starts here
                year++;
                prevloan = balance;
                newloan = prevloan;
                interest =  newloan * INTEREST;
                // add 3% to the loan
                newwages = wages + (wages * 5.0 /100.0);
                // calculate new wages by adding a pay rise of 5%
                net = newwages - NET;
                // deduct 10k from the wages
                repaid = net * REPAID;
                // from the remaining wages calculate 9% repayment
                //if (newwages <0) {
                  //  repaid =0;
                //} 
                // (if the remaining wages are zero or less, repayment is zero)
                newlinterest = newloan + interest;
                newbalance = newlinterest - repaid;
                // reapaid loan amount
                wagest = net;
                yrbal= newbalance * year;
                // sum up repayments and years so far
                
                System.out.println(" Year " + (int)year);
                MoneyOut.println(" Old Loan ", (int)balance);
                // produce output: current year, old loan, 
                MoneyOut.println(" Loans plus 3% interest ", newlinterest);
                // loan plus 3% interest, wages, 
                MoneyOut.println(" Wages ", (int)newwages);
                MoneyOut.println(" Wages minus threshold ", wagest);
                //wages minus 10,000 pounds, repayment, new loan
                MoneyOut.println(" Repayment ", (int)repaid);
                MoneyOut.println(" New loan amount ", newbalance);
                if (year >=30)
                {
                    System.out.println(0);
                }  // 'break' out of the 'while' loop if more than 30 years have passed
            }   // end of the 'while' loop, program leaves it when loan is 0 or less
                
                System.out.println(" It took you " +(int)year + 
                " years to repay the loan "); 
            // print out the number of year it took to repay the loan
            
                System.out.println(" The orginal loan was " + "" + loans + 
                " but you had to pay back " + yrbal);
            // print out the original loan and the total amount paid back
             
        } // end of main
    
    } // end of class LoanPayments
    The Money.out Class is part of the Program also I disable the If Statement Greyed out to see if that was causing the issue but it still won't Implement as required.
    Last edited by welsh_rocker; 01-12-2011 at 09:30 AM.

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    What do you expect this program to do? And what happens in reality when you run this?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Print out the value of variable 'newbalance' just before that while loop starts and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by welsh_rocker View Post
    but seem to be having problems with the While Loop it will run, but only go round 1 or maybe 2 times
    Are you pretty sure that your code gets inside this while loop, and iterates a couple of times before termination? In that case,
    Java Code:
    newbalance = newlinterest - repaid;
    this is the only place where your newbalance is getting modified. Evaluate all the three values in this statement with each iteration. They might tell you the cause of this.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by goldest View Post
    Are you pretty sure that your code gets inside this while loop, and iterates a couple of times before termination?
    It's easy to see that the body of the while loop is never executed:

    - newbalance is initialized to zero;
    - nothing is done with it until:
    - while condition tests if it is greater than zero.

    Boing, the entire while loop body is skipped.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    welsh_rocker is offline Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the feedback guys I can see what you what you mean with the code :) I will make some changes to the coding now, the aim of the program is the take the new loan balance from the 1st calculation, then read the new loan amount every time the calculation goes round till it reaches 0.

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Red face

    Quote Originally Posted by JosAH View Post
    It's easy to see that the body of the while loop is never executed:

    - newbalance is initialized to zero;
    - nothing is done with it until:
    - while condition tests if it is greater than zero.

    Boing, the entire while loop body is skipped.

    kind regards,

    Jos
    OOPS, my bad! :o
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  8. #8
    welsh_rocker is offline Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    7
    Rep Power
    0

    Default

    Hey guys which way would be the best to use the results for the new loan amount in the 1st set calculations and implement them within the Loop I have tried a few different methods and still have the result of 0 on the newloans variable even after declaring it with the results from the 1st set of calculations.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by welsh_rocker View Post
    Hey guys which way would be the best to use the results for the new loan amount in the 1st set calculations and implement them within the Loop I have tried a few different methods and still have the result of 0 on the newloans variable even after declaring it with the results from the 1st set of calculations.
    In your original code just initialize newbalance to any positive value so your while-loop body will be executed at least once; in the body you calculate a new value for newbalance (as you already do).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    welsh_rocker is offline Member
    Join Date
    Jan 2011
    Location
    Wales
    Posts
    7
    Rep Power
    0

Similar Threads

  1. java loop problem
    By filem_funk in forum New To Java
    Replies: 5
    Last Post: 11-10-2009, 03:56 AM
  2. Replies: 3
    Last Post: 07-18-2009, 01:26 PM
  3. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM
  4. The Assignment Problem
    By bumblyb33 in forum New To Java
    Replies: 5
    Last Post: 03-04-2009, 04:21 AM
  5. Replies: 7
    Last Post: 02-21-2009, 10:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •