-2

Possible Duplicate:
Add leading zeroes to number in Java?

For my program I have to make this types of code

i.e ABC000001, ABC000002 and so on,

First three characters are fixed and after that code should incremented,

code like :

int i = 000001;
String str = "ABC";

loop{
    String temp = str + i;
    i = i + 1;
}

when I used int 000001 consider as 1.

if 10 code will show like ABC000010 and if code will 100 then ABC000100, ABC000101 And So on...

So Any one can tell me what should I have to do to getting this type of Result.

Community
  • 1
  • 1
sachin
  • 1,447
  • 4
  • 14
  • 22

2 Answers2

4

This is a trivial job for String.format (since J2SE 5.0)

String temp = String.format("ABC%06d", i);
Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

You need to format the number using one of the java.text utilities, otherwise the number will be a "1", not a "000001".

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138