# While Loop

• 11-19-2010, 09:51 PM
lala
While Loop
:confused:
I am tryng to write a program that will calculate:
interest, and monthly payment.

The program should output the number of months it wold take to repay the loan.

Principle is 1000
Monthly payment is \$25
interest rate per month is 7.2/12=.6%

Problems:
My program is in a loop
My mind is in aloop. I have thoroughly confused myself.

Code:

```import java.util.*; import javax.swing.JOptionPane;          public class MonthlyPmts { public static void main(String[] args){                                                                                 /* Declaring the variables that will be used in the problem */                 String loanAmt;                 String intPerYr;                 String monthlyPmt;                 double interest;                 double principal = 1000;                 double intRateMon = .06;                 double monPay = 25;                                        while (principal <= 1000)         {                /* Perform the calculations  */        interest = (principal * intRateMon); monPmt = (principal - interest - monPay);                         }                /* Input Dialog boxes                          */                 loanAmt = JOptionPane.showInputDialog                                         ("Enter the loan amount: ");                        intPerYr = JOptionPane.showInputDialog                                         ("Enter the interest rate per year: ");                 monthlyPmt = JOptionPane.showInputDialog                                         ("Enter the monthly payment: ");                                         }         }```
• 11-19-2010, 10:44 PM
pbrockway2
Code:

```while (principal <= 1000)         {                /* Perform the calculations  */        interest = (principal * intRateMon); monPmt = (principal - interest - monPay);                         }```

Once it starts, that loop won't ever end. It keeps going while principal<=1000 and nothing in the loop changes the value of principal.

Work one small step at a time, compiling as often as you can. (literally after every line or block of code.) Respond to compiler messages as they occur. If you get stuck copy and post the entire compiler message. I mention this because the code you posted won't compile.

You explained the overall goal of your program but, to try and see what might be wrong with your code or suggest how loops and things might be used, we need more than the overall goal. We need to know how - by what steps or process - you mean to reach that goal. ("first I'm going to ... then I'll calculate ... using this formula ...")

This might even involve posting assignment instructions if the general plan of attack is outlined there. Again I'm motivated to say this because I have no clue what this loop is supposed to be doing: all I can do is offer the observation that, once started, it won't end.
• 11-20-2010, 02:30 AM
al_Marshy_1981
lala I agree with pbrockway, hard to tell what you are trying to achieve though his loop suggestions are bang on, please post a full description what your program is meant to do, where in your opinion it is falling down, and any errors/wrong output you are recieving...
• 11-20-2010, 01:38 PM
lala
While loop
Thank you everyone for your support.
Here are the program specs:

Suppose that you borrow \$1000 at the interest rate of 7.2% per year & the pmts. are monthly. Suppose that your monthly pmt. Is \$25. The interest is 7.2% per yr. & the pmts are monthly so the int. rate per mon. is 7.2/12 = 0.6% .The 1st month’s interest on \$1000 is 1000 * 0.006 = 6. Because the pmt. Is \$25 & interest for the first month is \$6, the pmt. toward the principal amt. is 25 – 6 = 19. This means that after making the first payment, the load amount is 1000 – 19 = 981. For the second payment, the interest is calculated on \$981. So the interest for the second month is 981 * 0.006 = 5.886 that is approx. \$5.89. This implies that the payment toward the principal is 25 – 5.89 = 19.11, & the remaining balance after the second payment is 981 – 19.11 = 961.89. This process is repeated until the loan is paid.
Write a program that accepts as input, the loan amount, the interest rate per year, & the monthly pmt. (Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2). The program then outputs the number of months it would take to repay the loan. (Note: If the monthly payment is less than the first month’s interest, then after each payment, the loan amount will increase. In this case, the program must warn the borrower that the monthly payment is too low & with this monthly payment the loan amount could not be repaid.)

After looking at my code I see why i'm looping.
Code:

`while (principal <= 1000)`
principal will never change, like pbrockway2 & al_Marshy_1981 said.

My lack of understanding loops shot my self-confidence way down. I do not know where to put the loop.
I will start again this afternoon. I will also post more detailed questions, & error messages.

Again thank you so much for your support. I know I will be able to write this program, with a group like this I can't go wrong. :)
• 11-20-2010, 09:17 PM
lala
While loop
As advised by wiser heads, I am writing my code in pieces. Neither of my System.out... statements print. I can enter an amount (loanAmt = console.nextDouble()), but that's it. No error messages. The program just sits there lwaiting for more input/data. (??)

Code:

```import java.util.*; import javax.swing.JOptionPane;          public class Monthly_Test {         static Scanner console = new Scanner(System.in);         public static void main(String[] args)         {                                                                                /* Declaring the variables that will be used in the problem */                 String loanAm;                 String intPerYr;                 String monthlyPmt;                 String outputStr;                 double loanAmt = 0;                 double inRateyr = 0;                 double monPmt = 0;                                 while (loanAmt != 0)                 {                 System.out.println ("Enter the loan amt");                 loanAmt = console.nextDouble();                        System.out.println("The Loan amount is:  " + loanAmt);                 }         } }```

P.S. I need to contact the site owners to let them know of a problem I am having when trying to log in. I could not find a "contact us" link.
Thanks
• 11-20-2010, 10:20 PM
pbrockway2
Code:

```double loanAmt = 0; double inRateyr = 0; double monPmt = 0;                 while (loanAmt != 0) {```

The first line in this portion of your code sets loanAmt to zero then the while loop says "do the following while loanAmt is not zero". In other words the loop will never execute.

Do you want to prompt for the loan multiple times? If not then you can get rid of the while loop and just use those three lines as they are.
• 11-21-2010, 03:39 PM
lala
While loop
Thanks for responding pbrockway2.
I have to use the "while".

If I don't initialize loanAmt I receive this error:
"Monthly_Test.java:18: variable loanAmt might not have been initialized
while (loanAmt != 0)
^
1 error

----jGRASP wedge2: exit code for process is 1.
• 11-21-2010, 04:16 PM
JosAH
Quote:

Originally Posted by lala
If I don't initialize loanAmt I receive this error:
"Monthly_Test.java:18: variable loanAmt might not have been initialized
while (loanAmt != 0)

Well, do as your compiler suggested: initialize the loanAmt variable, preferably to a non-zero value so your while loop enters its body for the first time (where you read in another value for that variable).

kind regards,

Jos
• 11-21-2010, 07:54 PM
pbrockway2
Quote:

I have to use the "while"

Perhaps you could post the actual conditions. What you posted in #4 doesn't suggest, much less require, that the input be accepted within a while loop.
• 11-22-2010, 05:38 AM
lala
While loop
Can someone tell me why when i run the code below the compiler does not generate the following messages, but as soon as I put in while (loanAmt > 0) it complains.
MonthlyPmts.java:21: variable loanAmt might not have been initialized
while (loanAmt > 0) {
^
MonthlyPmts.java:53: variable MostoRepay might not have been initialized
outputStr = "The number of months to payoff loan are : " + MostoRepay;
^
MonthlyPmts.java:55: variable monPmt might not have been initialized
if (monPmt > IntAmt)
^
MonthlyPmts.java:55: variable IntAmt might not have been initialized
if (monPmt > IntAmt)

Code:

```import java.util.*; import javax.swing.JOptionPane;         public class MonthlyPmts { static Scanner console = new Scanner(System.in); public static void main(String[] args){                                                                                 /* Declaring the variables that will be used in the problem */                 String inputStr;                 String outputStr;                 int NoMos;                 double MostoRepay;                 double IntRateYr;                 double loanAmt;;                 double IntRateMon;                 double IntRateMonC;                 double monPmt;                 double IntAmt;                 double NewLoanAmt;                                                                                 /* Input Dialog boxes                          */ inputStr = JOptionPane.showInputDialog         ("Enter the loan amount: "); loanAmt = Double.parseDouble(inputStr);                        inputStr = JOptionPane.showInputDialog                   ("Enter the yearly interest rate: "); IntRateYr = Double.parseDouble(inputStr);                 inputStr = JOptionPane.showInputDialog                           ("Enter the number of months : ");                 NoMos = Integer.parseInt(inputStr);                        inputStr = JOptionPane.showInputDialog                                         ("Enter the monthly payment: ");                 monPmt = Double.parseDouble(inputStr);                 IntRateMon = IntRateYr / NoMos;                 IntRateMonC = IntRateMon / 100;                 IntAmt = loanAmt * IntRateMonC;                 NewLoanAmt = loanAmt - IntAmt; MostoRepay = loanAmt / monPmt;   outputStr = "The number of months to payoff loan are : " + MostoRepay;                 if (monPmt > IntAmt)                         JOptionPane.showMessageDialog(null,outputStr,                                 "Months Remaing to Payoff Loan",                         JOptionPane.INFORMATION_MESSAGE);                 else                 outputStr = "Monthly payment is too LOW. With this monthly payment the loan amount could not be repaid";                         JOptionPane.showMessageDialog(null,outputStr,                                 "WARNING",                         JOptionPane.INFORMATION_MESSAGE);                                         /*        intPerYr = JOptionPane.showInputDialog                                         ("Enter the interest rate per year: ");                 monthlyPmt = JOptionPane.showInputDialog                                         ("Enter the monthly payment: ");                                                                 System.out.println(loanAmt);                                 System.out.println(monPmt);                                 System.out.println(MostoRepay);         outputStr = "Number of months to pay loan are: " + (MostoRepay = loanAmt / monPmt); */                         }         }```
pbrockway2 The input does not have to be in a "while" loop. It is probably my poor stated explanation. I need the while loop to calculate the loanAmt until it is zero.
• 11-22-2010, 07:01 AM
pbrockway2
Where are you putting the while loop to get this error? (In general it's a good idea to post the code that gives rise to the compiler's message)

It ought to go after those input dialogs - beacuse at that point you have all the information you need to start calculating things. And at that point "while(loanAmt>0)" should be good because loanAmt has been initialised by the first of the input dialogs.

I notice the other errors occur quite a few lines later - the compiler is not going to assume that the loop is ever entered, so if things get initialised within the loop they can't be used after it.
• 11-22-2010, 06:10 PM
lala
While loop
Thanks for responding. The code was included.