Results 1 to 9 of 9
Thread: Loop driving me loopy!!!!!
- 01-15-2009, 03:25 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 39
- Rep Power
- 0
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, 03:30 PM #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 ....
What does the program do?Java Code:public static void main(String args[]) { //main methods commands } public static calculate(double amount) { // while() { } }
- 01-15-2009, 03:53 PM #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
- 01-15-2009, 03:58 PM #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");
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, 04:25 PM #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"); } }
- 01-15-2009, 06:31 PM #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
- 01-15-2009, 10:46 PM #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. :)
- 01-16-2009, 12:34 AM #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:
I don't know if it's right, but it turns out that the loan gets paid back in 15 years, one month.Java Code:newBal=newBal-deduction;
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-16-2009, 01:00 AM #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.
The problem here is the variable loan which never changes. and newBal is set to loan on every loop cycle.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
Similar Threads
-
Applet to HTML Driving me bonkers
By proud2bhaole in forum Java AppletsReplies: 6Last Post: 04-14-2010, 02:08 PM -
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10-20-2008, 02:29 PM -
How to use Do While loop
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:45 PM -
Errors driving me crazy! although compiles fine
By irishsea2828 in forum New To JavaReplies: 1Last Post: 04-08-2008, 03:23 PM -
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12-22-2007, 10:20 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks