• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replace Random Positions in a String  RSS feed

 
Zamfir Adrian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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 !
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I am grateful, Thanks!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check my update.
 
Zamfir Adrian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Check my update.

I already did, thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!