# Loop driving me loopy!!!!!

Printable View

• 01-15-2009, 04:25 PM
soc86
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
• 01-15-2009, 04:30 PM
MuslimCoder
Ohh... You used are using the While statement as a method. you should put it in a method ....

Code:

``` public static void main(String args[]) { //main methods commands } public static calculate(double amount) { // while() { } }```
What does the program do?
• 01-15-2009, 04:53 PM
soc86
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
• 01-15-2009, 04:58 PM
soc86
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
• 01-15-2009, 05:25 PM
MuslimCoder
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)

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"); } }```
• 01-15-2009, 07:31 PM
soc86
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
• 01-15-2009, 11:46 PM
MuslimCoder
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. :)
• 01-16-2009, 01:34 AM
CJSLMAN
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:
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
• 01-16-2009, 02:00 AM
angryboy
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.
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.