• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Numberformater or substring or ????

 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If have a string that looks like this:

String test = "111122223333444";

I have to convert this string to:

String result = "1111-2222-3333-4444";

I now i can do this very easy, when i take a sbbstring 4 times like this:



But is there a easyer or better way, maybe with NumberFormatter, im running java 1.3 ???

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

I'm not sure what your parsing rules are exactly, but I'm guessing that you want a dash between every grouping of four digits for a number that is 16 digits long. A number that large can be handled by a java.math.BigDecimal. Here's my main method to test out a DecimalFormat approach (sorry, I did this in JDK 1.4.2, but try it out in 1.3 and see if it works):

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One problem here is, what if the number begins with zeros? E.g. "0000111122223333" will be written as "1111-2222-3333", which probably isn't what you'd want. It's possible to write code to prepend 0's and -'s as necessary, but this will end up being more complex than the initial solution with substring.

One possible problem with the substring() approach - is it possible that the string might be less than 12 digits? If the input is "123456", is that an error? Or should that be rendered as "0000-0012-3456"? The latter will require some extra coding. Hopefully this is an erroneous input you don't have to deal with, but it might be worth considering.
 
Frank Jacobsen
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks

That helpt me a lot !




Frank
 
It is an experimental device that will make my mind that most powerful force on earth! More powerful than this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic