• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to keep leading zeros when doing a parseInt

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a string which is a combination of a alpha prefix and a number suffix. I extract the prefix and get the number suffix of the string. Then, I do a Integer.parseInt on the suffix to get the value as an integer so that I can increment it. This works ok in general, but not when the suffix has leading zeros. In this case, I need to retain the leading zeros. So, for example, if the suffix is 001, when I parseInt it the integer value become 1 and then when I increment it, it becomes 2. I need it to be 001, 002.

Is there a way to do this?

Thanks in advance.
 
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After you increment it, convert it back to a String with those leading zeroes.
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem is I'm not ever sure when the suffix has leading zeros. It depends on what value is being sent in. Also, the suffix may have 1 zeros to n number of zeros, but I don't know that. So I could have:
01, 02, 03 or I could have 08, 09, 10.
 
Paul Clapham
Sheriff
Posts: 28371
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you mean you want the resulting String to be N characters, and you want to add leading zeroes until it reaches N characters, right? And you do know what N is, don't you?
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I do know what N is (length of String), but I don't know if the N contains leading zeros or not, and if it does how many leading zeros. So if I increment, I don't know how many zeros I should add or when I should stop adding zeros.

I suppose I could parse through the string to figure out how many zeros it has and stop when it exceeds the length of N?
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you know the length of the original string, the length of the prefix, and the length of the incremented-then-converted-to-a-string integer part, you know how many zeros to add:

input string: prefix0000123
total length:13
prefix length: 6
length of string_after_increment (124): 3

13 - 6 - 3 = 4
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use java.lang.DecimalFormat or String.format, you can specify the total number of digits. Whatever digits are not filled by the number itself are filled with zeros instead.
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An easy way to pad with leading 0's
Compute ix to get the number of leading 0's you want

String padded = ("0000000000000"+old).substring(ix); // get rightmost chars
 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the fact that String.format won't work on beginners':

int i = . . .;
String numberText = String.format("%010d", i);
 
Rita Chakras
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much for all your help. Fred's solution works well.
 
If you are using a rototiller, you are doing it wrong. Even on this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic