0

Is there a way to display if statements within JOptionPane? I tried using String.format and I also tried encapsulating the if statements in a method and then call the method with JOptionPane, but nothing worked. I can't figure out what I am doing wrong.

I would like something similar to the below console display

--- $1,042.18 is broken down as follows ---

    10 hundred dollar bills
     2 twenty dollar bills 
     2 one dollar bills    
     1 dime                
     1 nickel              
     3 pennies             

to show in a message dialog window.

My code is as follows:

import javax.swing.JOptionPane;

public class ChangeDisplayJOptionPane {
    public static void main(String[] args) {
        double amount, originalAmount;
        int hundredDollars, fiftyDollars, twentyDollars, tenDollars, 
            fiveDollars, oneDollars, quarters, dimes, nickels, pennies;

        String amountString = JOptionPane.showInputDialog("Enter an     amount:");
        originalAmount = Double.parseDouble(amountString);

        amount = originalAmount;
        amount *= 100;

        hundredDollars = (int)amount / 10_000;
        amount        %= 10_000;
        fiftyDollars   = (int)amount / 5_000;
        amount        %= 5_000;
        twentyDollars  = (int)amount / 2_000;
        amount        %= 2_000;
        tenDollars     = (int)amount / 1_000;
        amount        %= 1_000;
        fiveDollars    = (int)amount / 500;
        amount        %= 500;
        oneDollars     = (int)amount / 100;
        amount        %= 100;
        quarters       = (int)amount / 25;
        amount        %= 25;
        dimes          = (int)amount / 10;
        amount        %= 10;
        nickels        = (int)amount / 5;
        amount        %= 5;
        pennies        = (int)amount;

        JOptionPane.showMessageDialog(null, String.format("%n--- $%,.2f is broken down as follows ---%n%n",
                originalAmount));

        // Must display in dialog window
        System.out.printf("%n--- $%,.2f is broken down as follows ---%n%n",
            originalAmount);

        if (hundredDollars == 1)
            System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bill");
        else if (hundredDollars > 1)
            System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bills");
        if (fiftyDollars == 1)
            System.out.printf("%10s %-20s%n",fiftyDollars,"fifty dollar bill");
        if (twentyDollars == 1)
            System.out.printf("%10s %-20s%n",twentyDollars,"twenty dollar bill");
        else if (twentyDollars > 1)
            System.out.printf("%10s %-20s%n",twentyDollars,"twenty dollar bills");
        if (tenDollars == 1)
            System.out.printf("%10s %-20s%n",tenDollars,"ten dollar bill");
        if (fiveDollars == 1)
            System.out.printf("%10s %-20s%n",fiveDollars,"five dollar bill");
        if (oneDollars == 1)
            System.out.printf("%10s %-20s%n",oneDollars,"one dollar bill");
        else if (oneDollars > 1)
            System.out.printf("%10s %-20s%n",oneDollars,"one dollar bills");
        if (quarters == 1)
            System.out.printf("%10s %-20s%n",quarters,"quarter");
        else if (quarters > 1)
            System.out.printf("%10s %-20s%n",quarters,"quarters");
        if (dimes == 1)
            System.out.printf("%10s %-20s%n",dimes,"dime");
        else if (dimes > 1)
            System.out.printf("%10s %-20s%n",dimes,"dimes");
        if (nickels == 1)
            System.out.printf("%10s %-20s%n",nickels,"nickel");
        if (pennies == 1)
            System.out.printf("%10s %-20s%n",pennies,"penny");
        else if (pennies > 1)
            System.out.printf("%10s %-20s%n",pennies,"pennies");

        System.exit(0);
    }
}

Any help would be appreciated. Thanks in advance.

Will Peterson
  • 86
  • 3
  • 10
  • 1
    Display `if` statements within `JOptionPane`? What do you mean? – MikeCAT Mar 04 '16 at 04:07
  • Your output from the console does not have "if" statements. Please define why you said you wanted to display "if" statements. – Matt C Mar 04 '16 at 04:13
  • @MikeCAT - I'm trying to use `JOptionPane` and have the same formatted console display as shown above. – Will Peterson Mar 04 '16 at 04:26
  • 1
    @Matthew Cliatt - Maybe my wording is a little off. I am trying _not_ to display anything in the console. I would like everything to be displayed in windows using `JOptionPane`. – Will Peterson Mar 04 '16 at 04:34

1 Answers1

1

Instead of printing out each statement in your if's, like you have here:

    if (hundredDollars == 1)
        System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bill");
    else if (hundredDollars > 1)
        System.out.printf("%10s %-20s%n",hundredDollars,"hundred dollar bills");
    if (fiftyDollars == 1)
        System.out.printf("%10s %-20s%n",fiftyDollars,"fifty dollar bill");

You'll build a string, then put that string in the JOptionPane like this:

String str = "";        
    if (hundredDollars == 1)
        str += String.format(%10s %-20s%n",hundredDollars,"hundred dollar bill");
    else if (hundredDollars > 1)
        str += String.format(%10s %-20s%n",hundredDollars,"hundred dollar bills");
    JOptionPane.showMessageDialog(null, str, originalAmount, JOptionPane.INFORMATION_MESSAGE));

See the examples here: https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html

And refer to this post: Customize JOptionPane Dialog

For more information on JOptionPane's and customizing their look and feel.

Matt C
  • 4,470
  • 5
  • 26
  • 44
  • 1
    Thank you! That was exactly what I was looking for. It works perfect now. I tried building a String earlier with the if statements, but I doing it incorrectly. – Will Peterson Mar 04 '16 at 06:26