Results 1 to 9 of 9
  1. #1
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Question Loop driving me loopy!!!!!

    Any advise on what is wrong with my loop please?



    public static void main (String[]arg){
    //declare variables
    double loan;
    double wages;
    final int THRESHOLD= 10000;
    double netInc;
    double newBal=0;
    double totalLoan;
    double deduction;
    //print qoute
    System.out.println("Enter total amount of student loan taken");
    loan=UserInput.readDouble();
    //print qoute
    System.out.println("Enter anticipated income");
    wages=UserInput.readDouble();

    newBal=loan*0.03;

    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;

    totalLoan=loan+newBal;

    newBal=totalLoan-deduction;




    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction);
    System.out.println("New balance: "+newBal);


    }

    while (newBal >0.0){


    newBal=loan*0.03;

    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;

    totalLoan=loan+newBal;

    newBal=totalLoan-deduction;

    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction);
    System.out.println("New balance: "+newBal);
    }

    else {

    System.out.println("end ");

    }

    }




    }//end main

    }//end class

  2. #2
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Ohh... You used are using the While statement as a method. you should put it in a method ....

    Java Code:
     public static void main(String args[])
    {
    //main methods commands
    }
    
    public static calculate(double amount)
    {
    //
    
    while()
    {
    
    }
    }
    What does the program do?

  3. #3
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    It should calculate a loan over 1 year and if there is a balance left in new balance it should do the calculation again until the value of new balance is 0

  4. #4
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Sorry bit of a novice, do you mean i should write it like this?

    public static void main (String[]arg){

    double loan;
    double wages;
    final int THRESHOLD= 10000;
    double netInc;
    double newBal=0;
    double totalLoan;
    double deduction;


    System.out.println("Enter total amount of student loan taken");
    loan=UserInput.readDouble();

    System.out.println("Enter anticipated income");
    wages=UserInput.readDouble();

    newBal=loan*0.03;

    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;

    totalLoan=loan+newBal;

    newBal=totalLoan-deduction;




    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction);
    System.out.println("New balance: "+newBal);


    }
    public static calculate(double amount)
    {


    while(newBal >0.0){


    newBal=loan*0.03;

    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;

    totalLoan=loan+newBal;

    newBal=totalLoan-deduction;

    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction);
    System.out.println("New balance: "+newBal);
    }

    else {

    System.out.println("end ");

    }

    }




    }//end main

    }//end class

  5. #5
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    Everyone starts from being novice. I am actually no better, just trying to improve myself so I joined.

    I still am having trouble understanding the variables ... can you write the formula you used to get this.

    Here it is the code...it is stuck in infinite loop try checking the newbal value to see if it really gets below 0 (Zero)

    Java Code:
    import java.util.Scanner;
    
    public class Loaner
    {
    public static void main (String[]arg){
    
    double loan;
    double wages;
    final int THRESHOLD= 10000;
    double netInc;
    double newBal=0;
    double totalLoan;
    double deduction;
    
    // I dont know which class contains the UserInput so I will use Scanner 
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter total amount of student loan taken");
    loan = scan.nextDouble();
    
    System.out.println("Enter anticipated income");
    wages= scan.nextDouble();
    
    newBal=loan*0.03;
    
    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;
    
    totalLoan=loan+newBal;
    
    newBal=totalLoan-deduction;
    
    
    
    
    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction); 
    System.out.println("New balance: "+newBal); 
    
    
    
    while(newBal  > 0.0){
    
    
    newBal = loan*0.03;
    
    netInc=wages-THRESHOLD;
    deduction=netInc*0.09;
    
    totalLoan=loan+newBal;
    
    newBal=totalLoan-deduction; 
    
    System.out.println("Loan taken out: "+loan+" anticipated income "+wages);
    System.out.println("Total plus interest: "+totalLoan);
    System.out.println("Amount Re-paid: "+ deduction); 
    System.out.println("New balance: "+newBal); 
    }
    
    System.out.println("End"); 
    
    
    
    } 
    
    }

  6. #6
    soc86 is offline Member
    Join Date
    Oct 2008
    Posts
    39
    Rep Power
    0

    Default

    Yea no problem, below is my input and my expected out put:

    input

    Please enter the loan amount: 4000
    Please enter the initial wages: 13000


    Output

    YEAR: 1 Old loan: 4,000.00
    Loan plus 3% interest: 4,120.00
    Wages: 13,000.00
    Wages minus threshold: 3,000.00
    Repayment: 270.00
    New loan amount: 3,850.00

    YEAR: 2 Old loan: 3,850.00
    Loan plus 3% interest: 3,965.50
    Wages: 13,650.00
    Wages minus threshold: 3,650.00
    Repayment: 328.50
    loan amount: 3,637.00
    :
    :
    :
    :
    YEAR: 9 Old loan: 745.09
    Loan plus 3% interest: 767.44
    Wages: 19,206.92
    Wages minus threshold: 9,206.92
    Repayment: 828.62
    New loan amount: -61.18

    It took you 9 years to repay the loan
    The original loan was 4,000.00 but you had to pay back 4,801.08

    I have had the formula working from my last assignment, but i have been asked to develop this one further by adding a loop

  7. #7
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    I think it is going in a inifite loop because it does not update the "wages" and "load " after running the loop. so you should try adding a code that does that at the end of the loop. :)

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Not being a financial advisor...

    OK... here' s how the skeleton should look....
    Outside the loop (before entering the loop) you have to set up all the necessary variables. It looks like you pretty much have done that. Some questions:
    • I'm assuming that the salary stays the same (no increases), right?
    • none of the variables suffer from inflation/deflation/bankrupcy/recession/etc, right?
    • If the above is true, then the ONLY thing that is changing each year is the newBal variable which is deducted a constant 270.00 Pounds, right?

    Then the only thing that needs to be in the loop are the variables that change. So... replace everything in the loop (except the printlns) with:
    Java Code:
    newBal=newBal-deduction;
    I don't know if it's right, but it turns out that the loan gets paid back in 15 years, one month.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    haha, my calculation is way off. 21 years. But at least it exits the loop. It would be easier if you just post the original math formular for these calculations.

    from MuslimCoder's codes, here are the first couple of outputs from the console.
    Java Code:
    S:\>java Loaner
    Enter total amount of student loan taken
    4000
    Enter anticipated income
    13000
    Loan taken out: 4000.0 anticipated income 13000.0
    Total plus interest: 4120.0
    Amount Re-paid: 270.0
    New balance: 3850.0
    Loan taken out: 4000.0 anticipated income 13000.0
    Total plus interest: 4120.0
    Amount Re-paid: 270.0
    New balance: 3850.0
    Loan taken out: 4000.0 anticipated income 13000.0
    Total plus interest: 4120.0
    Amount Re-paid: 270.0
    New balance: 3850.0
    The problem here is the variable loan which never changes. and newBal is set to loan on every loop cycle.

Similar Threads

  1. Applet to HTML Driving me bonkers
    By proud2bhaole in forum Java Applets
    Replies: 6
    Last Post: 04-14-2010, 03:08 PM
  2. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  3. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  4. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 04:23 PM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Posting Permissions

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