Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to incriment 0001 to 0002 in java?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Henry Wong
author
Marshal
Pie
Posts: 21413
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

Seetharaman Venkatasamy
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably 1 is decimal and 0001 is octal?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic