• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Replace Random Positions in a String

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys, I'm working to a system called Quiz ...

The last thing that remains are the 'clues'. In present i have


and I want to remove from xml the clues because is hard to do them manually ...
so I made something but i failed



if someone offers to help me I would be very grateful..

-Thanks !
 
lowercase baba
Posts: 13069
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you be more specific than "I failed"? Does your code compile? Do you get error messages? Does it crash when it runs? THe more information you give us, the easier it is to help you (and folks are more likely to do so).
 
Zamfir Adrian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:Can you be more specific than "I failed"? Does your code compile? Do you get error messages? Does it crash when it runs? THe more information you give us, the easier it is to help you (and folks are more likely to do so).


oh, sorry..I didn't received an error...

Compile Resutl => 'Einst*in'

the loop doesn't work.. idk .. only the first letter from array is replaced...
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're using some random thingy (Rnd) to determine whether or not to replace. Perhaps for "e" (one occurrence) it returned something over 50 and for "i" it didn't?
I've tried your code (using another random number returning method instead of Rnd.get) and I got "Einstein", "E*nste*n", "Einst*in", "Einst**n" and more results like that.

That last one is because you never reset start and end when you get a new letter. That means that when you replace "e" first, the next replacement will only start after that. Perhaps you can use a StringBuilder (not StringBuffer) instead for the entire contents, then replacing only that part:


Oh, and UseRealWords: "I don't know" instead of "idk".
 
Zamfir Adrian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am grateful, Thanks!
 
Rob Spoor
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check my update.
 
Zamfir Adrian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Check my update.


I already did, thanks again
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic