
/*
Online Java - IDE, Code Editor, Compiler

Online Java is a quick and easy tool that helps you to build, compile, test your programs online.
*/

import java.util.*;

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        String test="of example a test string";
        
        censor cs = new censor ();
        System.out.println("This is the string: " + cs.censor(test));
       
    }
}

class censor
{
    private String test;
    StringBuffer sb=new StringBuffer(" ");
    int numberChars;
    StringJoiner sj = new StringJoiner(" ");
    boolean wordLengthGreaterFour=false;
    String converted;
    
    public String censor (String test)
    {
    
    this.test=test;
    String temp;
    
    
    StringTokenizer st = new StringTokenizer(test);
   
        while (st.hasMoreTokens())   //looks for more words
        {
            temp=st.nextToken();
            System.out.println("Processing word: " + temp);
            
            
            if (temp.length()<=4)   //length is equal or less than 4
            {
                // this will add the convertedtoken using stringjoiner which has a delimiter of blank space 
                
                numberChars=temp.length();
                System.out.println(numberChars);
                System.out.println("length token should be less than 4: " + numberChars);
                
                converted = temp.toString();
                System.out.println("This is the token less than or equal to 4 chars: " + converted);
                sj.add(converted);
                System.out.println("This is the current string: " + sj.toString());
                System.out.println("\n");
                
            }
            else
            {
                numberChars=temp.length();
                System.out.println("length token shuld be greater than 4: " + numberChars);
                
                converted = temp.toString();
                System.out.println("This is the token greater than 4: " + converted);
                
                wordLengthGreaterFour=true;
                
                 for (int j=0; j<numberChars; j++)
                 {
                     System.out.println("Processing * " + (j+1) + " of " + (numberChars));
                     sb.append("*");
                }
                
                System.out.println("This is the number of *: " + sb);
                
                sj.add(sb);  //this will add the stringbuffer string as a string into stringjoiner
                
                System.out.println("This is the current stringbuilder with ****:  "  +  sb);
                sb.replace(0,sb.length(), "");   // this will now clear contents of the stringbuffer
                
                System.out.println("This is the current stringbuilder with **** REMOVED:  "  +  sb);
                
                System.out.println("This is the current string: " + sj.toString());
                
                System.out.println("\n");
                
            }
            
        }
        
        if (!wordLengthGreaterFour)   // this is if none of the words are greater than 4 characters
        {
            sj.add(test);
            return sj.toString();
        }
        return sj.toString();
    
}
}
