| 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: 16692
|
|
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: 9948
|
|
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.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
santhosh.R gowda
Ranch Hand
Joined: Apr 06, 2009
Posts: 296
|
|
Thanks
Seetharaman Venkatasamy
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32689
|
|
|
Presumably 1 is decimal and 0001 is octal?
|
 |
 |
|
|
subject: How to incriment 0001 to 0002 in java?
|
|
|