Random replacement of letter

Joined
Sep 18, 2006
Messages
1
Reaction score
0
Hi,

can somebody tell me why this doesn't work:

public static void main(String[] args)
{
String s = "0101";
int i = (int)(Math.random()*3);
if(s.charAt(i) == 'a')
{
s.replace(s.charAt(i), 'x');

}
else
{
s.replace(s.charAt(i), 'y');
}

System.out.println(s);
}

I am trying to replace randomly one of the letters in the string. Is there alternative way?

Note: I do not want to change every '0' or '1', but only that one whose index matches random integer (i)

Thanks...
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top