Adblock breaks this site

Need Some Java Help With a Loan Calculator

Discussion in 'Programming General' started by iceblits, May 27, 2011.

  1. iceblits

    iceblits Active Member

    Joined:
    May 10, 2007
    Posts:
    135
    Referrals:
    0
    Sythe Gold:
    0
    Need Some Java Help With a Loan Calculator

    I'm trying to make a loan program that calculates the number of months it takes to pay off a loan while also printing out the Payment, Balance, Interest, and Principle. Heres what I have so far..



    Code:
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JOptionPane.*;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    public class MonthlyPayment {
    	public static void main(String[] args) {
    		// Declaration of variables
    		double I; // Monthly interest rate
    		double Balance = (double) -1; // Amount borrowed
    		double InterestRate = (double) -1; // Annual interest rate
    		double MonthlyPayment = (double) -1; // Monthly payment
    		double Interest; // Amount owed through interest
    		double TotalInterest=(double) 0; //
    		double Principle=(double) 0; // Part that goes towards paying off the loan
            int Payment=0;
    		NumberFormat form;
            DecimalFormat DF=new DecimalFormat("###00");
            form=NumberFormat.getCurrencyInstance();
    		
           // Prompting the user for the loan, annual interest, and monthly payment
            while (Balance <= 0) { // Prompting positive value greater than zero
    			String Prompt = "Enter the Loan Amount:";
    			String Data = JOptionPane.showInputDialog(Prompt, "0");
    			if (Data == null)
    				return;
    			Balance = Double.parseDouble(Data);
    		}
    
    		while (InterestRate <= 0) {
    			String Prompt = "Enter the Annual Interest Rate:";
    			String Data = JOptionPane.showInputDialog(Prompt, "0");
    			if (Data == null)
    				return;
    			InterestRate = Double.parseDouble(Data);
    		}
    		while (MonthlyPayment < 0) {
    			String Prompt = "Enter the Monthly Payment:";
    			String Data = JOptionPane.showInputDialog(Prompt, "0");
    			if (Data == null)
    				return;
    			MonthlyPayment = Double.parseDouble(Data);
    		}
    		//calculations
    		I=InterestRate/12;
    		System.out.println();
    		while(Balance>MonthlyPayment){
    			Interest=Balance*I;
    			TotalInterest=TotalInterest+Interest;
    			Principle=MonthlyPayment-Interest;
    			System.out.println(Payment+""+DF.format(Balance)+""+DF.format(Interest)+""+DF.format(Principle));
    		    Payment=Payment+1;
    		    
    		}
    		
    	}
    	
    
    }
    
    its stuck in an infinite loop for some reason....
     
  2. Fate1

    Fate1 Apprentice
    Banned

    Joined:
    Apr 21, 2005
    Posts:
    773
    Referrals:
    2
    Sythe Gold:
    5
    Need Some Java Help With a Loan Calculator

    It is your last loop in your program. You never update your variable "Balance" so it will always stay greater than MonthlyPayment.
     
  3. iceblits

    iceblits Active Member

    Joined:
    May 10, 2007
    Posts:
    135
    Referrals:
    0
    Sythe Gold:
    0
    Need Some Java Help With a Loan Calculator

    alright..i updated it by adding the following to the while loop:

    Balance=Balance-Principle

    but now it prints out really strange numbers...like when i type in 1000 for the loan amount, .09* for interest rate, and 90 for the monthly payment, the output is:


    09180882
    18340783
    27510684
    36660684
    45810585
    54960486
    64090486
    73220387
    82350288
    91470288
    10580189
     
  4. iceblits

    iceblits Active Member

    Joined:
    May 10, 2007
    Posts:
    135
    Referrals:
    0
    Sythe Gold:
    0
    Need Some Java Help With a Loan Calculator

    ahh ic..i added spaces in between and i now get the output:

    0 918 08 82
    1 834 07 83
    2 751 06 84
    3 666 06 84
    4 581 05 85
    5 496 04 86
    6 409 04 86
    7 322 03 87
    8 235 02 88
    9 147 02 88
    10 58 01 89

    but the balance, interest, Principle are in incorrect number formats..i want them to be decimal currency
     
  5. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Need Some Java Help With a Loan Calculator

    Code:
    	NumberFormat form;
            DecimalFormat DF=new DecimalFormat("###00");
            form=NumberFormat.getCurrencyInstance();
    
    You created a NumberFormat object for currency (money) purposes that you don't use and you tried to create a DecimalFormat object that you do use but isn't doing what you want.

    Just use the NumberFormat object you created instead of the DecimalObject one since it's specifically designed for money there's no point trying to create one yourself. After that, remove the unneeded DecimalFormat variable and you're done.

    Spoiler alert:
    0 $917.50 $7.50 $82.50
    1 $834.38 $6.88 $83.12
    2 $750.64 $6.26 $83.74
    3 $666.27 $5.63 $84.37
    4 $581.27 $5.00 $85.00
    5 $495.63 $4.36 $85.64
    6 $409.34 $3.72 $86.28
    7 $322.41 $3.07 $86.93
    8 $234.83 $2.42 $87.58
    9 $146.59 $1.76 $88.24
    10 $57.69 $1.10 $88.90
     
  6. iceblits

    iceblits Active Member

    Joined:
    May 10, 2007
    Posts:
    135
    Referrals:
    0
    Sythe Gold:
    0
    Need Some Java Help With a Loan Calculator

    awesome..it worked! thank you so much
     
< [WIP]Java Tower Defence | What Type Of Programming is used for xbox 360 and PS3 game ? >


 
 
Adblock breaks this site