Printf and scanf in java new String formatting features in Java 1.5

printf is probably the best known function in C. I did some
programming in C and I didn’t really like it (because I started with C after
quite some experience in Java). But one thing was better than Java, the printf
function. The printf function replaces tokens that start with % with one of the
arguments (usally dynamic values). Strings with dynamic values are more
clarifying and easier to maintain:

printf(“I am %d years old”,24) versus printf(“I am ” + 24 +
” years old”);


Before I start explaining printf you need to know a little
bit about varargs.

One of the reasons the printf function wasn’t possible in
Java was the lack of a method with variable arguments. With the printf function
in C you can pass an arbitrary number of arguments, in Java you had to put
everthing in an array of Objects. And while doing that concatenating a String the old way was shorter and easier.

With Java 1.5 varargs were introduced. The syntax is a bit

public int sum(int … x){

    int sum=0;

    for(int i=0;
i<x.length; i++){



    return sum;


Just add three dots before the name of the variable and
you’re ready. x is now an array of int’s. The type of the variable can be both
native and Object. There are a few restrictions. There can only be one varagrs argument and it should be the last argument in a method.


The PrintWriter object has a method printf since Java 1.5.
It works the same as the printf function in C. Let’s show some basic examples:

System.out.print(“%f cm is %f inch”, 2.54, 1F);

Output: 2,540000
cm is 1,000000 inch

%f is used for floats and doubles. Just like C you can
prepend the f with the number of decimals and the precision:

System.out.printf(“%9.2f cm is %10d inch\n”, 2.54,

System.out.printf(“%5.2f cm is %f inch\n”, 2.54,

When the number is smaller than the amount you provided the
output is padded with spaces:

     2,54 cm is          1 inch

 2,54 cm is 1,000000

Printf only works on PrintWriter objects. Of course we want
it to work on Strings. Use String.format for the same behaviour as the printf
method. Besides %f there are a lot of characters you can use. These are the
most common:


Decimal integers


Decimal numbers




Escaping for percent sign

is a list of other characters you can use.

Date format

Another nice thing of the new formatter is date formatting.
Of course we can do that with SimpleDateFormat, but I think that’s too many
lines of code for a simple thing:

date = new Date();

format = new SimpleDateFormat(“dd-MM-yyyy”);

string = format.format(date);

date: %s”, string));

To do this with the formatter:

date=new Date();

date: %1$td-%1$tm-%1$tY”,date));

The syntax might scare you at first sight, but it’s quite

1$ means pick the first argument, I do this because there is
only one argument and we need three (the date consists of 3 parts). Using an
index of an argument also works without date formatting.

t means pick the time and date formatting characters

d m Y are used for day, month and year padded with 0’s.

For the other characters check:


The Scanner class works with input. You can specify a
delimiter of tokens (default is a space) and then iterate over the tokens and
put them in the right object type. Example:

input = “1 car 2 bike 3 ship”;

scanner = new Scanner(input);

int a =

b =;

int c =

d =;

int e =

f =;


%s %d %s %d %s”, a, b, c, d, e, f);

First we create an input string with 3 int’s and 3 Strings.
Then we create a scanner object on that input string. Then we iterate over the
objects and put them in objects with the right type. At then end we reconstruct
the String with printf.

It’s not always safe to use nextInt when there could be a
String (or if you just want to skip all Strings and only save ints. You can use
the hasXxx methods for this. A simple method to get all the ints in a list
would look like this:

List<Integer> extractInts(String input) {

    Scanner scanner = new Scanner(input);

    List<Integer> list = new


    while (scanner.hasNext()) {

        if (scanner.hasNextInt()) {


        } else {




    return list;


With the useDelimeter method you can change the delimiter of


The performance of the new String formatter is slower than
plain string concatenation. I tested a couple of million times with
concatenating 3 Strings of about 100 characters and that was 5 times slower
with String.format(“%s%s%s”,a,b,c);.

The difference between the date format examples is however
only 8% slower (with 3 million tests)

So be careful when you apply the String formatter. It’s
considerably slower. But for logging purposes it’s great.


Java 1.5 gives use some new powerful String features.
Unfortunately the performance is a bit disappointing, but maybe in newer
versions of Java the performance will improve. I use the new functions a lot
for logging. The Scanner class is quite new for me, but I expect to use it a

One thought on “Printf and scanf in java new String formatting features in Java 1.5

  1. It was good to see your article. Thanks!!
    Oh, and please change conlusion to  conclusion!

Comments are closed.

Next Post

What do we need to find out at ODTUG 2006?

Facebook0TwitterLinkedinThis weekend, the show goes on again: ODTUG 2006 – The Kaleidoscope of Oracle Development Technology. Our AMIS delegation will do seven presentations, including one on behalf of Oracle Corporation (Steven unfortunately cannot attend and asked me to present on JHeadstart). We hope to not only present our own stories […]