Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Print 000 infront of String, I can get INT to work but not string

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Print 000 infront of String, I can get INT to work but not string

    Hi All,

    I have look thru internet on how to print the 0000 in front of string but i could not find it. I can print out integer without any problem. The code i using for int number is:-

    int x = 101
    String.format("%1$08d", x));

    Output:
    00000101

    But if i want to print something like C or 10C to be 0000000C and 0000010C i cannot use the above function.

    Anyone can help with this?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Print 000 infront of String, I can get INT to work but not string

    As you can see from the Formatter documentation there is no way to pad a string with leading zeros. That comment is a little tongue in cheek: the documentation is a rather involved if you haven't seen the like before. But it is a good place to start and ask about if you are unsure.

    There are formatting commands that will right align a string to a given width padding the left hand end with spaces.

    public class LeadingZeros {
        public static void main(String[] args) {
            String test = "some string";
     
            String formatted = String.format("%20s", test);
            System.out.println("-->" + formatted + "<--");
        }
    }


    You could make a StringBuilder with the formatted string and then replace leading spaces with zero characters in a for loop.

    -----

    Your example, 10C --> 0000010C, looks suspiciously not like any old string but, in fact, the hexidecimal form of an int value. In that case notice that the documentation talks about an x and an X conversion which perform a hexadecimal format of an int - in much the same way as d performs a decimal format.

    public class LeadingZeros {
        public static void main(String[] args) {
            //int test = 0x10C;
            int test = 268; // same int
            String formatted = String.format("%08X", test);
            System.out.println("-->" + formatted + "<--");
        }
    }

    (If 10C is intended to be a numerical value it is more appropriate to declare and use it an an int, not a String.)
    Last edited by pbrockway2; June 1st, 2012 at 11:26 PM.

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. Replies: 7
    Last Post: December 11th, 2011, 10:58 PM
  3. How to change a String value into a number and then back into a String.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 18th, 2011, 12:43 PM
  4. [SOLVED] difference between String Concatenation and String -Buffer/Builder .append(<value>)
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 3rd, 2011, 08:16 AM
  5. Replies: 18
    Last Post: March 2nd, 2011, 09:52 AM