1

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);
    }
  }
}
deltab
  • 2,498
  • 23
  • 28
wu binhao
  • 11
  • 1
  • 6
  • Welcome to Stack Overflow, and thank you for your question! I've submitted an edit that indents the source code and adds a title that describes the problem. – deltab Jul 22 '17 at 23:08
  • Related, maybe even duplicate: https://stackoverflow.com/a/29054406/3182664 – Marco13 Jul 23 '17 at 02:00

2 Answers2

1

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              
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
  • More specifically, formatting with `"%-20s"` appends spaces, so `"Total Payment\n"` is formatted as `"Total Payment\n␣␣␣␣␣␣"` with the spaces at the start of the next line. – deltab Jul 22 '17 at 23:17
  • @deltab Yes, that's a nice explanation. – RaffleBuffle Jul 22 '17 at 23:26
0

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);
DavidGaray
  • 26
  • 1