Sometimes you may need to have leading zeros on an integer in java. It is easy to do this using formatting. The example shown below uses printf but anything that uses Formatter will achieve the same results. There are caveats though when reading in from a file for example. Lets look at the sample code first and discuss the caveats later.
The code is a simple loop that runs on the printable range of the ASCII table. The results are the int value, character printed, hex value, whether the character is a legal Java identifier and whether the character can legally be the start of a Java identifier. The Character wrapper class contains the methods used to test legality but it is the formatting used in the printf statement on line 11 that's relevant to this post.
This results in output like this:
Many will be familiar with printf and formatter usage such as %s calling the corresponding int attribute e.g.
This prints: hello hello hello hello hello hello
The printf statement in line 11 of the sample code adds a little bit more to the mix. Firstly the 1$ is used to specify we are using the first attribute (if there was more then 1 attribute you can use 2$, 3$ etc to reuse existing attributes)i.e. the following code for the hellos above would achieve the same output:
The next thing is the 04 the 4 is the width as usual but the 0 is key. It is a flag that says to pad the result with zeros as shown in the above output. So if using following code:
outputs: 42 042 0042 00042
The
Java docs describes it as follows:
The format specifiers for general, character, and numeric types have the following syntax:
%[argument_index$][flags][width][.precision]conversion
The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.
Now the caveat. Say, for example you wrote an int padded with zeros to a file. You look at the file and everything is as it should be, nice padded numbers. Then you go to read it back in and you start getting weird results. This is because Java treats ints with leading zeros as Octal. There are a few ways around this (read the field in as a String or float and parse/cast) but that is a subject for another day.
In conclusion padding with zeros is easy if you need to do it. However it may be wise not to use zero padding of integers at all or ensure the use is carefully documented/commented due to the way Java handles it. The Oracle Java tutorial on number formatting can be found
here.
Download commented example
here (Pastebin).
Simples.