aspose file tools*
The moose likes Beginning Java and the fly likes How to incriment 0001 to 0002 in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to incriment 0001 to 0002 in java?" Watch "How to incriment 0001 to 0002 in java?" New topic

How to incriment 0001 to 0002 in java?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear All,

I'm having one requirement such that 0001 should be incremented to 0002 but when we use regular + operator its giving 2 instead of 0002 how to add including zeros also

Creativity is nothing but Breaking Rules
Henry Wong

Joined: Sep 28, 2004
Posts: 18108

If you are referring to integer types, then try to print it out before you increment it. You will notice that it is printed as 1 and not 0001. Integer types do not have a format, you'll need to add it yourself during printing.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

yes, as henry suggested, you apply format. in Java 5.0 you can do like below,

<edit>changed the format %03d to %04d</edit>

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10909

There is a difference in the VALUE, and how it get's DISPLAYED. the number '2' has the same value regardless of me writing it as "2", "two", "II", or any other way.

So you really have two requirements.

1) You need to increment the value of '1' to '2'
2) You need to display these values with some leading zeros.

The best way to approach this is to figure out how to do each individually, and only THEN figure out how to combine those steps into a single program.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296

Seetharaman Venkatasamy
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36478
Presumably 1 is decimal and 0001 is octal?
I agree. Here's the link:
subject: How to incriment 0001 to 0002 in java?
Similar Threads
why i get the incorrect data from mySQL????
Removing a node through DOM
Classid on object tag
Regarding Database Transction sychronisation
Collections Framework