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

Need to replace all integers in a string with new random integers , java newbie and struggling with

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

String uuid = "LFD69EED05F011E683B38B7A62A9F7BEG";

Need to randomize just the integers in the above string (replace with new random integers)..

Any help would be appreciated.

Played with .replace and .replaceall with little success.

Thanks.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you trying to do this?

What did you try with replaceAll() that didn't work?
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can build a regex and match the string with this regex,whenever it finds a match replace the match with any random integer.take a pen and paper,try to build the logic accordingly with i have mentioned,if you find things difficult at some point don't hesitate to put a reply then.

kind regards,
praveen.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In pseudo code
Convert given string to char[]
Iterate each
if char is a number, replace value in char array (simple substitution lile 1=A,2=B or whatever the requirement is)
Once done reconvert/process char[]
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have just tried another method using concatenation its working you can use it
DEMO:-

 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a poor solution Praveen. It will only solve the problem for one specific case, and it's very verbose.

You should be able to implement a method like this in one line of no more than 80 characters:
 
praveen kumaar
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:That's a poor solution Praveen. It will only solve the problem for one specific case, and it's very verbose.

You should be able to implement a method like this in one line of no more than 80 characters:



I am thinking the same thing thanks for making me sure stephan,even it needs lots of effort if string grows its length  somewhat around 100,1000 or more.i think using regex will be far better than this which i adviced initially.

Thanks! again Stephan for your opinion.

kind regards,
praveen.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, disregard the one line part in my last post. I made a big mistake in my reasoning. The best way to solve this problem is probably what Maneesh proposed.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:In pseudo code
Convert given string to char[]
. . .

Convert the String to a StringBuilder surely?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can the new digit be the same as the old digit?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic