Pregunta

I asked a question early and got some great help :D My first question. I thought I was out of the woods but I guess not. I am setting up/formation the information that my program is getting but I am running into a problem when trying to print my variable "time" if any one could point out what I am doing wrong I would be much obliged.

the section of code I believe to be the problem is:

// this class prints a loan statement showing the amount borrowed
    // and the amount borrowed, the annual interest rate, the number of months
    // and the monthly payment
    public static void loanStatement(double principle, double interest, int time, double mPayment)
    {
       System.out.printf("%2s%13s%8s%20s%n", "Principle", "Interest", "Time", "Monthly Payment");
       System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);
       System.out.println("monthly payment is" +mPayment);
       System.out.println("interest is" +interest);
       System.out.println("Time is" +time);

when I take out the +time and remove the 10.2f (I tried many different combinations) I get no errors. I also had it with the time and Monthly payment variables being printed and got a weird mesh of numbers.

I put in the other printlns because I wanted to check that the program was returning the proper values and it is.

below is the formatting errors I am getting

Principle     Interest    Time     Monthly Payment
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '10.2f'
15000.00     36.07  at java.util.Formatter.format(Formatter.java:2487)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at loanpayment.LoanPayment.loanStatement(LoanPayment.java:89)
    at loanpayment.LoanPayment.main(LoanPayment.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

I hope what I am asking make sense to you guys, and thanks for any help in advance!

¿Fue útil?

Solución

You are providing only two inputs here:

   System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);

whereas it requires three. Maybe you are missing to put the comma(,) before +time Try this if logical for you:

   System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest, +time);

Otros consejos

You're missing a comma in your format...

System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest +time);
                                                              ^--- Missing comma

Which is trying to add the last two values, try

System.out.printf("%2.2f%10.2f%10.2f%n", +principle, +interest, +time);

instead...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top