Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. Formatting using Printf()

```
//import Scanner
import java.util.Scanner;

public class Program3{

public static void main(String[] args) {

//intiate the Scanner
Scanner input;
input = new Scanner(System.in);

//declare loan amount
System.out.print("Enter Loan Amount: ");
double loanAmount = input.nextDouble();

//declare int NumOfYears
System.out.print("Number Of Years: ");
int NumOfYears = input.nextInt();

//declare annual interest rate
double AnnualInterestRate = 5;  // intital

System.out.println("Interest Rate: \t Monthly Rate:\t    Total Payment: \t");
//intial loop
while (AnnualInterestRate <= 8) //test
{
//Monthly
double MonthlyInterestRate = AnnualInterestRate/1200;

//Calculate monthly payment
double monthlyPayment = loanAmount * MonthlyInterestRate / (1 - (Math.pow(1 / (1 + MonthlyInterestRate), NumOfYears * 12)));

//calculate total payment
double totalPayment = monthlyPayment * 12 * NumOfYears;

//ouput of total, monthly, annual
System.out.print(AnnualInterestRate + ("          \t") + monthlyPayment + "   \t" + totalPayment);
System.out.printf("%8.2f", AnnualInterestRate , "8.2f" , monthlyPayment, "%8.2f", totalPayment );
System.out.println();
// change

AnnualInterestRate += .125;

} //while
}//main
}// class```

I'm really not sure what I'm doing wrong, I've read up on the printf() and formatting but just can't seem to get this to format right. It needs to come out to two decimal places and should stay in a 3 column output. Please any suggestions?

2. Re: Formatting using Printf()

The printf() method takes a String followed by the values that replace the format Specs IN the string:

System.out.printf( "%8.2f %8.2f %8.2f", AnnualInterestRate, monthlyPayment, totalPayment );

3. Re: Formatting using Printf()

Thank you man.

--- Update ---

Thank you man