Hi all,
I would like to make a method that replace from a string the character k with x if the number of characters k is bigger than the x number, and the opposite replace.
My code is :
class Replacing_class
{
public static String changeString(String str)
{
char letter;
int len;
int counter_x=0,counter_k=0 ;
len = str.length();
for (int i = 0; i < len; i++)
{
letter = str.charAt(i);
if (letter == 'x')
counter_x++;
else if (letter == 'k')
counter_k++;
}
if(counter_k > counter_x)
str.replace('k', 'x');
else if (counter_x > counter_k)
str.replace('x', 'k');
else
str="FALSE";
return str;
}
public static void main(String[] args)
{
String s1="xxx kkk x aa";
String s2="kkkk kkk x aa";
System.out.println(changeString(s1));
System.out.println(changeString(s2));
}
}
I don't understand why this does not work.
what am I doing wrong ?
If I had had a file name "New_File.txt" with 50 row
how can I call the method for every string and replace the characters ?
Thanks a lot