# %6d???

• 01-29-2011, 09:24 PM
PrimalScientist
%6d???
Hi All,

What exactly does the %6d do?

Many thanks,

Daz.
• 01-29-2011, 10:40 PM
Fubarable
Please read the link in my signature on how to ask smart questions as yours is missing lots of detail necessary to be answerable. For instance in what context do you mean? Regular Expressions? String formatter (such as with printf and String.format(...))?
• 01-29-2011, 10:48 PM
PrimalScientist
Smart?

Sorry for being vague there. You are correct.

Code:

``` /**  * This program simulates a vending machine that gives change  *  * @Darren  * @20110129  */ import java.util.Scanner; public class VendingMachine {   public static void main(String[] args)   {     Scanner in = new Scanner(System.in);         final int PENNIES_PER_DOLLAR = 100;     final int PENNIES_PER_QUATER = 25;         System.out.print("Enter bill value (1 = \$1 bill, 5 = \$5 bill, etc.): ");     int billValue = in.nextInt();     System.out.print("Enter item price in pennies: ");     int itemPrice = in.nextInt();         // Compute change due         int amountDue = PENNIES_PER_DOLLAR * billValue - itemPrice;     int dollarCoins = amountDue / PENNIES_PER_DOLLAR;     amountDue = amountDue % PENNIES_PER_DOLLAR;     int quaters = amountDue / PENNIES_PER_QUATER;         // Print change due         System.out.printf("Dollar coins change is %6d", dollarCoins);     System.out.println();     System.out.printf("Quaters change is: %6d\n", quaters);     System.out.println();                 }       }```
This is the code that %6d belongs in. I just wanted to know what it actually does?

Thanks all. Again, apologies for bring vague there. I just thought it was some form of code!!

Daz.
• 01-29-2011, 11:21 PM
Fubarable
The API will help you out here. If you look up the System class, you'll see that the out field is a PrintStream object, and its method printf will display formatted Strings in exactly the same way as the Formatter class. In fact you'll have a link to the Formatter API, the syntax subsection, which I'll also provide here: Formatter API, syntax section. I suggest you have a look at this. The d means that you are trying to format a decimal integer, the %6 means the width of the String. So this:

Code:

`System.out.printf("Dollar coins change is %6d", dollarCoins);`

Will display the String Dollar coins change is xxxxxxx
and where I have the xxxxxx's you'll see the number that dollarCoins holds displayed in a field width of 6. Play with the number to see how changing it changes your output, and again, check out the API link that I've provided above.

Luck.
• 01-29-2011, 11:31 PM
PrimalScientist
That's great.

I will do as you actually said in your first reply and check out the Formatter API.