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