Gareth
Gareth

Reputation: 2200

String.format won't allow int

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?

Upvotes: 4

Views: 2905

Answers (4)

NPE
NPE

Reputation: 500933

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+).

Upvotes: 1

Raja P
Raja P

Reputation: 3

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

Upvotes: 0

TechSpellBound
TechSpellBound

Reputation: 2555

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.

Upvotes: 5

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136162

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)

Upvotes: 4

Related Questions