Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 965
Gareth Avatar asked Dec 26 '22 10:12

Gareth


1 Answers

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.

like image 146
TechSpellBound Avatar answered Jan 08 '23 15:01

TechSpellBound