1. Member
Join Date
Jan 2011
Posts
27
Rep Power
0

## %6d???

Hi All,

What exactly does the %6d do?

Many thanks,

Daz.

2. 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(...))?
Last edited by Fubarable; 01-29-2011 at 10:47 PM.

3. Member
Join Date
Jan 2011
Posts
27
Rep Power
0
Smart?

Sorry for being vague there. You are correct.

Java 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.

4. 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:

Java 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.

5. Member
Join Date
Jan 2011
Posts
27
Rep Power
0
That's great.

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

Daz.

Ooh, I got a lot to learn here LOL! :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•