Results 1 to 5 of 5

Thread: %6d???

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

    Default %6d???

    Hi All,

    Please help!!

    What exactly does the %6d do?

    Many thanks,

    Daz.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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 09:47 PM.

  3. #3
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    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. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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. #5
    Join Date
    Jan 2011
    Posts
    27
    Rep Power
    0

    Default

    That's great.

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

    Thanks for your help Luck.

    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
  •