Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'?

RahulS picture RahulS · Jun 11, 2015 · Viewed 7.2k times · Source

Can you please tell me where is the problem? I am not using concatenation here and the error is still there.

Here is the StackTrace:

`adding $Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
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 accounttest.main(accounttest.java:18)`

Here is the code which I am talking about:

import java.util.*;
import java.io.*; 
public class accounttest{

public static void main(String[] args) {
    account account1 = new account(50.00);
    account account2 = new account(-7.50);

    System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
    System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());

    Scanner input = new Scanner(System.in);
    double depositAmount;

    System.out.printf("Enter deposit amount for account 1\n>>");
    depositAmount = input.nextDouble();
    System.out.printf("\nadding $%.2f to account 1 balance\n\n");
    account1.credit(depositAmount);

    //displaying the current amount in both of the accounts

    System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
    System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());

    System.out.printf("Enter deposit amount for account 2\n>>");
    depositAmount = input.nextDouble();
    System.out.printf("\nadding $%.2f rupees to account 2 balance\n\n");
    account2.credit(depositAmount);

    System.out.printf("account 1 balance: $%.2f\n", account1.getBalance());
    System.out.printf("account 2 balance: $%.2f\n", account2.getBalance());

}
}

Answer

Reimeus picture Reimeus · Jun 11, 2015

As shown in the stacktrace add a format parameter where its missing as you've already done in the other statements

System.out.printf("\nadding $%.2f to account 1 balance%n", depositAmount);
                                                           ^