Results 1 to 3 of 3

Thread: if else?

  1. #1
    johnsonjp34 is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default if else?

    I'm trying to get this calculator to show loan payment values even in the event of a 0% interest situation. Presently it just spits out NaN when I use values of 0. I'm guessing I could use an if else around the "return payment" to get it to try another formula if the original goes to zero. How would I write this?

    Thanks,

    J
    Java Code:
    package com.direction.investor.financial.essentials;
    
    import java.io.Serializable;
    import java.text.DecimalFormat;
    
    public class LoanCalculator implements Serializable {
    
    	
    	private static final long serialVersionUID = 1L;
    	private double principal;
    	private double annualRate;		// in percentage
    	private int periodsPerYear;
    	private int years;
    
    	public static void main(String[] args) {
    		LoanCalculator aCal = new LoanCalculator();
    		aCal.setPrincipal(200000.0);
    		aCal.setAnnualRate(8);			// 8%
    		aCal.setPeriodsPerYear(12);
    		aCal.setYears(30);
    
    		System.out.println("Payment=" + aCal.computePayment());
    	}
    
    	public double computePayment() {
    		double payment = 0;
    		int totalPeriods = years * periodsPerYear;
    
    		payment = principal
    				* (annualRate/100)
    				/ (periodsPerYear * (1.0 - Math.pow(1.0 + (annualRate/100)
    						/ periodsPerYear, -totalPeriods)));
    
    		return payment;
    	}
    
    	public String[][] computeAmortization(){
    		double balance=principal;
    		double pmt=computePayment();
    		int totalPeriods=years * periodsPerYear;
    		double periodInterest=annualRate/(periodsPerYear*100);
    		double cumulatedPmt=0;
    		double cumulatedInterest=0;
    		DecimalFormat aFormat=new DecimalFormat("#,##0.00;(#,##0.00)");
    		String[][] aRtn=new String[totalPeriods][7];
    		for(int i=0; i<totalPeriods; i++){
    			double periodInterestAmt=balance*periodInterest;
    			cumulatedPmt+=pmt-periodInterestAmt;
    			cumulatedInterest+=periodInterestAmt;
    			aRtn[i][0]=String.valueOf(i);
    			aRtn[i][1]=aFormat.format(balance);
    			aRtn[i][2]=aFormat.format(pmt-periodInterestAmt);
    			aRtn[i][3]=aFormat.format(periodInterestAmt);
    			aRtn[i][4]=aFormat.format(cumulatedPmt);
    			aRtn[i][5]=aFormat.format(cumulatedInterest);
    			balance=balance-(pmt-periodInterestAmt);
    			aRtn[i][6]=aFormat.format(balance);
    		}
    		return aRtn;
    	}
    
    	public double getPayment() {
    		return computePayment();
    	}
    
    	
    	public double getAnnualRate() {
    		return annualRate;
    	}
    
    	
    	public int getPeriodsPerYear() {
    		return periodsPerYear;
    	}
    
    	
    	public double getPrincipal() {
    		return principal;
    	}
    
    	
    	public int getYears() {
    		return years;
    	}
    
    	
    	public void setAnnualRate(double annualRate) {
    		this.annualRate = annualRate;
    	}
    
    	
    	public void setPeriodsPerYear(int periodsPerYear) {
    		this.periodsPerYear = periodsPerYear;
    	}
    
    	
    	public void setPrincipal(double principal) {
    		this.principal = principal;
    	}
    
    	
    	public void setYears(int years) {
    		this.years = years;
    	}
    
    }
    Last edited by quad64bit; 10-24-2012 at 11:17 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: if else?

    Sounds like you're trying to divide by zero or something similar - I would put in a check for special cases (like the 0 case) or any other special edge cases.

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: if else?

    Well your culprit is here:

    payment = principal * (annualRate/100) / (periodsPerYear * (1.0 - Math.pow(1.0 + (annualRate/100) / periodsPerYear, -totalPeriods)));

    But, what happens if the annual rate is negative? I would think you'd want to avoid that too.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •