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 Spring in Action this week in the Spring 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
Author

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
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


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.

Henry


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>

hth
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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
Thanks

Seetharaman Venkatasamy
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Presumably 1 is decimal and 0001 is octal?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to incriment 0001 to 0002 in java?