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

Because Strings are immutable. You cannot change it's content, you can only create a new String containing modified content, so

str.replace('x', 'k');

should, of course, be

str = str.replace('x', 'k');

Because Strings are immutable. You cannot change it's content, you can only create a new String containing modified content, so

str.replace('x', 'k');

should, of course, be

str = str.replace('x', 'k');

Thanks a lot for your help.
I am trying to call this method for a file names NewFile.txt but the contents of this file
are not replaced but deleted.

import java.io.*;

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 = str.replace('k', 'x');
            else if (counter_x > counter_k)
              str = str.replace('x', 'k');
            else
              str="FALSE";
            
                
          return str;
         }
    
        
        
        public static void main(String[] args) 
       {
        
           try { 
               
               BufferedReader in = new BufferedReader(new FileReader("NewFile.txt")); 
               BufferedWriter out = new BufferedWriter(new FileWriter("NewFile.txt")); 
               String str; 
               String writestr; 
               int counter = 0;
               
               while ((str = in.readLine()) != null) 
               { 
                   if(counter == 50)
                      break;
                    
                   writestr = changeString(str); 
                   out.write(writestr);
                   out.write("\n");
                   
                   counter++;
                } 
                
                in.close(); 
                out.close();
            
            } catch (IOException e) { } 
        
        
       }
}

Thanking you in advance

If you need to change the contents of the file in-place, you'd need to:
- Use RandomAccessFile class
- Open the file in read/write mode
- Play around with file pointers (see javadocs for more details) and overwrite the existing content

I personally wouldn't recommend this approach due to the sheer amount of complexity involved in managing file pointers and keeping track of things.

Unless you are running under strict space/time constraints, a simpler approach would be to:
- Open the existing file using some Reader
- Create a new temporary file
- Read the text from the opened file and write the modified text to the newly opened file
- After the reading activity is complete, close the streams, delete the existing file and rename the temporary file so that it now becomes the original file
- Profit?! :)

If you need to change the contents of the file in-place, you'd need to:
- Use RandomAccessFile class
- Open the file in read/write mode
- Play around with file pointers (see javadocs for more details) and overwrite the existing content

I personally wouldn't recommend this approach due to the sheer amount of complexity involved in managing file pointers and keeping track of things.

Unless you are running under strict space/time constraints, a simpler approach would be to:
- Open the existing file using some Reader
- Create a new temporary file
- Read the text from the opened file and write the modified text to the newly opened file
- After the reading activity is complete, close the streams, delete the existing file and rename the temporary file so that it now becomes the original file
- Profit?! :)

Thanks a lot for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.