aspose file tools
The moose likes Beginning Java and the fly likes Replace Random Positions in a String Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Replace Random Positions in a String" Watch "Replace Random Positions in a String" New topic
Author

Replace Random Positions in a String

Zamfir Adrian
Greenhorn

Joined: Feb 18, 2011
Posts: 5
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

Joined: Oct 02, 2003
Posts: 10040
    
    6

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).


Never ascribe to malice that which can be adequately explained by stupidity.
Zamfir Adrian
Greenhorn

Joined: Feb 18, 2011
Posts: 5
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

Joined: Oct 27, 2005
Posts: 19232

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".


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Zamfir Adrian
Greenhorn

Joined: Feb 18, 2011
Posts: 5
I am grateful, Thanks!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

Check my update.
Zamfir Adrian
Greenhorn

Joined: Feb 18, 2011
Posts: 5
Rob Spoor wrote:Check my update.

I already did, thanks again
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Replace Random Positions in a String
 
Similar Threads
Set character formatting in Excel using POI
Line Breaks when displaying HTML
How to avoid out of memory error in string builder in android
How would I dump the contents of a table to text?
can any one explain this