File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
How to incriment 0001 to 0002 in java?
Joined: Apr 06, 2009
Jul 01, 2010 05:25:34
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
Joined: Sep 28, 2004
Jul 01, 2010 05:33:55
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.
Java Threads, 3rd Edition
Jini in a Nutshell
Java Gems (contributor)
Joined: Jan 28, 2008
Jul 01, 2010 05:54:19
yes, as henry suggested, you apply format. in Java 5.0 you can do like below,
String formatValue = String.format("%04d", intValue);
<edit>changed the format %03d to %04d</edit>
Joined: Oct 02, 2003
Jul 01, 2010 06:14:37
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
Joined: Apr 06, 2009
Jul 05, 2010 22:17:47
Joined: Oct 13, 2005
Jul 07, 2010 04:27:36
Presumably 1 is decimal and 0001 is octal?
I agree. Here's the link:
subject: How to incriment 0001 to 0002 in java?
Classid on object tag
why i get the incorrect data from mySQL????
Regarding Database Transction sychronisation
Removing a node through DOM
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014