简体   繁体   中英

output formatting Java

my question is simple, how can i get my output to format two values with two decimal places. for whatever reason i cant output the "current balance" & "new balance" with two decimal places. separately they work fine but when together i get a conversion error. is this a rule i'm unaware of? i would like do do this with one action if possible. this is simply a cosmetic issue and all operations perform fine when i take out the formatting.

thanks,

public static void main(String[] args) 
{
    double min;
    int accNum;
    char accType;
    double bal;
    double newBal;

    //user inputs
    System.out.println("Please enter your account number: ");
    accNum = console.nextInt();
    System.out.println();

    System.out.println("Enter the account type: s(savings) or c(checking)");
    accType = console.next().charAt(0);
    System.out.println();

    //savings v checking
    switch (accType)
    {
        case 's':
        case 'S':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            { 
                newBal = bal - S_MIN_FEE;
                System.out.println("Insufficient Funds (-$10.00)");
            }
            else
                newBal = bal + (bal * S_INT);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Savings" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f", newBal);
        case 'c':
        case 'C':
            System.out.println("Enter the current balance: ");
            bal = console.nextDouble();

            System.out.println("Enter the minimum balance: ");
            min = console.nextDouble();
            System.out.println();

            if (bal < min)
            {
                newBal = bal - C_MIN_FEE;
                System.out.println("Insufficent Funds (-$25.00)");
            }
            else if (bal < C_BAL_MAX && bal >= min)
                newBal = bal + (bal * C_INT);
            else
                newBal = bal + (bal * C_INT_MAX);

            System.out.printf("Account #: " + accNum + "\n" 
                    + "Account Type: Checking" + "\n" + "Current Balance: "
                    + "$%.2f%n", bal + "New Balance: $%.2f%n", newBal);

That is because you put the format first, and only first.

System.out.printf("%.2f %.2f%n", bal, newBal);

This is the same problem/mistake as posted an hour ago. java.util.IllegalFormatConversionException: f != java.lang.String Error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM