# Thread: Loop driving me loopy!!!!!

1. Member
Join Date
Oct 2008
Posts
39
Rep Power
0

## Loop driving me loopy!!!!!

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");
//print qoute
System.out.println("Enter anticipated income");

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. Senior Member
Join Date
Jan 2009
Posts
119
Rep Power
0
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. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
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. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
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");

System.out.println("Enter anticipated income");

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. Senior Member
Join Date
Jan 2009
Posts
119
Rep Power
0
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. Member
Join Date
Oct 2008
Posts
39
Rep Power
0
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. Senior Member
Join Date
Jan 2009
Posts
119
Rep Power
0
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. ## 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

9. 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.

#### Posting Permissions

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