# Display nothing if == 0

Mark Vermeer

Greenhorn

Posts: 2

posted 13 years ago

I am having a problem with the following exercise out of my University text book. I have figured out how to do everything except how to not display 0 amounts. Any help would be appreciated. Please bear with the length.

Thanks.

import javax.swing.JOptionPane;

public class Exercise3_1 {

/** Main method */

public static void main(String[] args) {

int amount; // Amount entered from the keyboard

// Receive the amount entered from the keyboard

String amountString = JOptionPane.showInputDialog(null,

"Enter an amount in integer, for example 1156",

"Compute Change", JOptionPane.QUESTION_MESSAGE);

// Convert string to integer

amount = Integer.parseInt(amountString);

int remainingAmount = amount;

// Find the number of one dollars

int numOfOneDollars = remainingAmount / 100;

remainingAmount = remainingAmount % 100;

// Find the number of quarters in the remaining amount

int numOfQuarters = remainingAmount / 25;

remainingAmount = remainingAmount % 25;

// Find the number of dimes in the remaining amount

int numOfDimes = remainingAmount / 10;

remainingAmount = remainingAmount % 10;

// Find the number of nickels in the remaining amount

int numOfNickels = remainingAmount / 5;

remainingAmount = remainingAmount % 5;

// Find the number of pennies in the remaining amount

int numOfPennies = remainingAmount;

String dollar = "";

String quarter = "";

String dime = "";

String nickel = "";

String penny = "";

if (numOfOneDollars == 0)

dollar = "";

else

dollar = (numOfOneDollars == 1) ? " Dollar" : " Dollars";

if (numOfQuarters == 0)

quarter = "";

else

quarter = (numOfQuarters == 1) ? " Quarter" : " Quarters";

if (numOfDimes == 0)

dime = "";

else

dime = (numOfDimes == 1) ? " Dime" : " Dimes";

if (numOfNickels == 0)

nickel = "";

else

nickel = (numOfNickels == 1) ? " Nickel" : " Nickels";

if (numOfPennies == 0)

penny = "";

else

penny = (numOfPennies ==1) ? " Penny" : " Pennies";

// Display results

String output = "Your amount " + amount + " consists of \n" +

numOfOneDollars + dollar + " \n" +

numOfQuarters + quarter + " \n" +

numOfDimes + dime + " \n" +

numOfNickels + nickel + " \n" +

numOfPennies + penny;

JOptionPane.showMessageDialog(null, output,

"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

Thanks.

import javax.swing.JOptionPane;

public class Exercise3_1 {

/** Main method */

public static void main(String[] args) {

int amount; // Amount entered from the keyboard

// Receive the amount entered from the keyboard

String amountString = JOptionPane.showInputDialog(null,

"Enter an amount in integer, for example 1156",

"Compute Change", JOptionPane.QUESTION_MESSAGE);

// Convert string to integer

amount = Integer.parseInt(amountString);

int remainingAmount = amount;

// Find the number of one dollars

int numOfOneDollars = remainingAmount / 100;

remainingAmount = remainingAmount % 100;

// Find the number of quarters in the remaining amount

int numOfQuarters = remainingAmount / 25;

remainingAmount = remainingAmount % 25;

// Find the number of dimes in the remaining amount

int numOfDimes = remainingAmount / 10;

remainingAmount = remainingAmount % 10;

// Find the number of nickels in the remaining amount

int numOfNickels = remainingAmount / 5;

remainingAmount = remainingAmount % 5;

// Find the number of pennies in the remaining amount

int numOfPennies = remainingAmount;

String dollar = "";

String quarter = "";

String dime = "";

String nickel = "";

String penny = "";

if (numOfOneDollars == 0)

dollar = "";

else

dollar = (numOfOneDollars == 1) ? " Dollar" : " Dollars";

if (numOfQuarters == 0)

quarter = "";

else

quarter = (numOfQuarters == 1) ? " Quarter" : " Quarters";

if (numOfDimes == 0)

dime = "";

else

dime = (numOfDimes == 1) ? " Dime" : " Dimes";

if (numOfNickels == 0)

nickel = "";

else

nickel = (numOfNickels == 1) ? " Nickel" : " Nickels";

if (numOfPennies == 0)

penny = "";

else

penny = (numOfPennies ==1) ? " Penny" : " Pennies";

// Display results

String output = "Your amount " + amount + " consists of \n" +

numOfOneDollars + dollar + " \n" +

numOfQuarters + quarter + " \n" +

numOfDimes + dime + " \n" +

numOfNickels + nickel + " \n" +

numOfPennies + penny;

JOptionPane.showMessageDialog(null, output,

"Example 2.4 Output", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

}

}

Mark Vermeer

Greenhorn

Posts: 2

Jason Menard

Sheriff

Posts: 6450

posted 13 years ago

You might want to work on this section of your code:

If I understand you correctly, if all you had was one dollar, then you would want your output to be simply "Your amount $1.00 consists of one dollar".

If that's the case, let me give you a couple of hints to point you in the right direction. You should actually check out the StringBuffer class to do this with, since it's more efficient. In any event, what you might think about when building your output String is checking

If you initialize your StringBuffer eariler, you actually already have your

HTH

[ November 07, 2003: Message edited by: Jason Menard ]

If I understand you correctly, if all you had was one dollar, then you would want your output to be simply "Your amount $1.00 consists of one dollar".

If that's the case, let me give you a couple of hints to point you in the right direction. You should actually check out the StringBuffer class to do this with, since it's more efficient. In any event, what you might think about when building your output String is checking

**if**your amount of a particular currency is greater than zero, and if it is, append the appropriate text to your String or StringBuffer.If you initialize your StringBuffer eariler, you actually already have your

*if*statements in place to determine whether or not to append anything to the StringBuffer, and what you should append.HTH

[ November 07, 2003: Message edited by: Jason Menard ]