2 Replies - 1640 Views - Last Post: 10 March 2011 - 09:26 PM Rate Topic: -----

#1 WyoWild   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-March 11

Java Mortgage calculator no columnar output

Posted 06 March 2011 - 07:28 PM

My mortgage calculator program calculates correctly but the output is not in
columns. Any suggestions?
import java.io.Console;
	import java.io.*;
	import static java.lang.Math.*;
	import java.util.Scanner;
	import java.text.DecimalFormat;
	import java.text.NumberFormat;
/**
 * @(#)AmortWeek3.java
 *
 * AmortWeek3 application
 *
 * @author  
 *			Week 3-Service Request SR-mf-003
 *			       Change Request 1
 *					Display breakdown of monthly payments
 *
 * @version 1.00 2011/3/4
 */

public class AmortWeek3 {

     public static void main(String[] args) {

       // Mortgage Calculation Formula Payment = (principal * interest) / (1 -- (1 + interest)^-number of payments)

        //Declaring the variables that will be used in the program
	    double monthlyPmt;							//Monthly Payment
	    double principalAmt = 200000;				//Amount of principal
	    double intRate = .0575;						//Annual interest rate
	    double monthlyIntRate = intRate / 12;		//Monthly interest rate
	    double loanTerm = 360;						//Total number of months
	    double counter = 0;								//Counter

	//Calculate monthly payment
	monthlyPmt = (principalAmt*(monthlyIntRate*(Math.pow((1+monthlyIntRate), loanTerm)
               )))/((Math.pow((1+monthlyIntRate),loanTerm))-1);

	DecimalFormat Currency = new DecimalFormat("$#,##0.00");

      //Display monthly payment
      System.out.println("Amoritized monthly payment for McBride");
      String payment = Currency.format(monthlyPmt);
      System.out.print(payment);
      System.out.println();
      System.out.println();

	      //Formula needs to broken down from (principal * interest rate) to calculate the interest for length of loan

		//Columnar headers
	    final int PAYMENT_WIDTH = 10;
	    final int AMOUNT_WIDTH = 15;
	    final int PRINCIPAL_WIDTH = 15;
	    final int INTEREST_WIDTH = 15;
	    final int BALANCE_WIDTH = 15;

	String pattern = "%" + PAYMENT_WIDTH + "s%" + AMOUNT_WIDTH + "s%" + PRINCIPAL_WIDTH + "s%" + INTEREST_WIDTH + "s%" + BALANCE_WIDTH + "s";

	System.out.printf(pattern, "PAYMENT", "AMOUNT", "PRINCIPAL", "INTEREST", "BALANCE");

	System.out.println();

	NumberFormat nf = NumberFormat.getCurrencyInstance();

	for (int month = 1; month <= loanTerm; month++) {

	double amountInterest = principalAmt * monthlyIntRate;

	double amountPrincipal = monthlyPmt - amountInterest;

	principalAmt = principalAmt - amountPrincipal;

	System.out.printf(pattern, month, nf.format(monthlyPmt), nf.format(amountPrincipal), nf.format(amountInterest), nf.format(principalAmt));

           if (month == 25) {
           	month =0;
           try {
           Scanner keyIn = new Scanner(System.in);
	 	System.out.print("(Press ENTER to see more months");
           System.in.read();                                     // Look for keyboard input
           } catch (IOException e) {                             // Catch the input
           return;                                               // Continue the program
           }
           month= month+1;                                      // Increment line counter
           } else {

	}
	 }
	}
}



Edited by macosxnerd101: Please, :code:.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Mortgage calculator no columnar output

#2 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Java Mortgage calculator no columnar output

Posted 07 March 2011 - 05:54 PM

Read the documentation for Class Formatter which is used by System.out.printf()
Was This Post Helpful? 0
  • +
  • -

#3 WyoWild   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 06-March 11

Re: Java Mortgage calculator no columnar output

Posted 10 March 2011 - 09:26 PM

View Postn8wxs, on 07 March 2011 - 05:54 PM, said:

Read the documentation for Class Formatter which is used by System.out.printf()

Thanks! I got it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1