Results 1 to 12 of 12

Thread: While Loop

  1. #1
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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.
    Please help. Please turn me around. :confused:

    Java 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: ");
    					 
    }
    	}

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

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

  4. #4
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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.
    Java 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. :)

  5. #5
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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. (??)

    Java 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

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

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

  7. #7
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by lala View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  10. #10
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

    Default 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)

    Java 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. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  12. #12
    lala is offline Member
    Join Date
    Oct 2010
    Posts
    80
    Rep Power
    0

Similar Threads

  1. Loop Help Please
    By JavaAssistance in forum New To Java
    Replies: 11
    Last Post: 09-26-2010, 10:05 PM
  2. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  3. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  4. Loop, is it what I need?
    By dbashby in forum New To Java
    Replies: 4
    Last Post: 04-14-2009, 04:28 AM
  5. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 10:15 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
  •