0

Here's what I'm trying to do:

String output = "If you borrow" + currencyFormatter.format(loanAmount);
    +" at an interest rate of" + rate + %;
    +"\nfor" + years;
    +",you will pay" + totalInterest + "in interest.";
mymiracl
  • 583
  • 1
  • 16
  • 24

1 Answers1

1

Take out the semicolons before the end of your concatenation.

String output = "If you borrow" + currencyFormatter.format(loanAmount)
    +" at an interest rate of" + rate + "%"
    +"\nfor" + years
    +",you will pay" + totalInterest + "in interest.";

I also recommend that you move the concatenation operator to the end of the line rather than the start of the line. It's a minor stylistic preference...

String output = "If you borrow" + currencyFormatter.format(loanAmount) +
    " at an interest rate of" + rate + "%" + 
    "\nfor" + years +
    ",you will pay" + totalInterest + "in interest.";

Finally, you may notice that you are missing some white-spaces when you try printing that string. The String.format method helps with that (also see the documentation for Formatter). It's also faster than doing lots of concatenations.

String output = String.format(
    "If you borrow %s at an interest rate of %d%%\nfor %d years, you will pay %d in interest.", currencyFormatter.format(loanAmount), rate, years, totalInterest
);
nasukkin
  • 2,460
  • 1
  • 12
  • 19