wu binhao
wu binhao

Reputation: 11

Why is the first row of this `printf`-formatted table not aligned?

I have a problem formatting my code to present the table properly and I am not sure how to fix it. Basically, the first row of numbers is not aligned properly to form columns like the rest. And how do I add the sign "%" at the end of the numbers under interest rate? Sorry, I tried to show what the output looks like here, but I cannot display it properly.

public class Boo {

  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Loan amount: ");
    double loanAmount = in.nextInt();

    System.out.println("Number of years: ");
    int year = in.nextInt();

    double monthlyPayment = 0;
    double interestRate = 5.0;
    final double INCREMENT = 0.125;

    System.out.printf("%-20s   %-20s   %-20s", "Interest Rate", "Monthly    Payment", "Total Payment\n");

    for (int i = 0; i < year; i++) {
      interestRate += INCREMENT; //increment by 0.125 every year

      double monthlyInterestRate = interestRate / 1200;

      monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, year * 12));
      double totalPayment = monthlyPayment * year * 12;  
      System.out.printf("%-20.3f   %-20.2f   %-20.2f\n", interestRate, monthlyPayment, totalPayment);
    }
  }
}

Upvotes: 1

Views: 263

Answers (2)

DavidGaray
DavidGaray

Reputation: 26

Double %% will get the single % in the output - I also noticed it fixes the left indentation on the number columns:

System.out.printf("%-20.3f%%   %-20.2f%%   %-20.2f%%\n", interestRate,
        monthlyPayment, totalPayment);

Upvotes: 0

RaffleBuffle
RaffleBuffle

Reputation: 5455

The misalignment of the first column was due to the newline at the end of the "Total Payment" header. It needs to be on the end of the format string.

Due to the left justification of the interest rate you need to create an intermediate string, formatted with a % sign, then left justify this.

I believe this code should work.

public static void main(String[] args)
{

    Scanner in = new Scanner(System.in);

    System.out.println("Loan amount: ");
    double loanAmount = in.nextInt();

    System.out.println("Number of years: ");
    int year = in.nextInt();

    double monthlyPayment = 0;
    double interestRate = 5.0;
    final double INCREMENT = 0.125;

    System.out.printf("%-20s   %-20s   %-20s\n", "Interest Rate", "Monthly    Payment", "Total Payment");

    for (int i = 0; i < year; i++)
    {
        interestRate += INCREMENT; // increment by 0.125 every year

        double monthlyInterestRate = interestRate / 1200;

        monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, year * 12));
        double totalPayment = monthlyPayment * year * 12;
        System.out.printf("%-20s   %-20.2f   %-20.2f\n", String.format("%.3f%%", interestRate), monthlyPayment, totalPayment);

    }

}

Output:

        Loan amount: 
        567
        Number of years: 
        5
        Interest Rate          Monthly    Payment     Total Payment       
        5.125%                 10.73                  643.95              
        5.250%                 10.77                  645.90              
        5.375%                 10.80                  647.86              
        5.500%                 10.83                  649.82              
        5.625%                 10.86                  651.79              

Upvotes: 1

Related Questions