4

I was looking for a quick easy way to format a int into a string with two leading zeros. I found this https://stackoverflow.com/a/4377337/47690 answer and it looked like what I needed. So I implemented it as such

int i = 34; //could be any value but you get the idea   
String.format("%03d", i);

But, Eclipse seems to moan about the fact that String.format requires an Object[] for its second parameter. What is going on here?

Community
  • 1
  • 1
Gareth
  • 2,180
  • 5
  • 19
  • 24

4 Answers4

5

If you want to print the value of i, you can use:

System.out.printf("%03d", i);

instead of

System.out.println(String.format("%03d", i));

EDIT :

Also I tried your code in Java 6 and it did not work. So, I used printf(). Ohh. sorry! I cleaned the project and it worked.

I am using Java 6 and Eclipse Helios.

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
  • Useful tip, but doesnt answer the question :). Strangely I tried in Java 6 and it still compiled in Eclipse.. hmm – cowls Jan 24 '13 at 09:36
4

Check you project settings

project -> Properties -> Java Compiler -> Compiler compliance level

you are probably in Java 1.4 which does not recognize vararg

String format(String format, Object ... args)
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

The following compiles and runs fine under Java 7 (and Eclipse Juno SR1 is happy with it):

public class Main {
    public static void main(String[] args) {
        int i = 42;   
        System.out.println(String.format("%03d", i));
    }
}

The error message is a red herring: even though the final argument is Object... (aka Object[]), auto-boxing takes care of everything.

I suspect you're using an outdated version of Java and/or Eclipse, or the compiler compliance level in your Eclipse project is set to pre-1.5 (even if you are using 1.5+).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • It could be the language level of the project. If it is set to an older version, auto unboxing might be disabled. – dunni Jan 24 '13 at 09:51
0

String s = String.format("%04d", i);

This code stands for 4 digits in a number to string so.. if u use %04d i will get two trialing zeros up front

Though int i is a primitive and its expecting object as its argument,

its JVM will takes care internally to converts into object data type

see this as per java implementation ..

public static String format(String format, Object ... args) {
return new Formatter().format(format, args).toString();
}

Sample code for appending zeros dynamically...

import java.text.DecimalFormat; public class ArrayTest {

public static void main(String[] args) {
    int i = 34; //could be any value but you get the idea   
    int zeroCount = 2;

    String s = String.format("%d", i);
    int length = s.length()+zeroCount;

    System.out.println(String.format("%0"+length+"d", i));

    // second way u can achieve 
    DecimalFormat decimalFormat = new DecimalFormat();
    decimalFormat.setMinimumIntegerDigits(length);
    System.err.println(decimalFormat.format(i));
}

}

And coming to the arguments of System.format it can take infinite no. of parameters as its a varargs object as second parameter

Check this url http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29

Raja P
  • 3
  • 6