/*
Online Java - IDE, Code Editor, Compiler

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

public class Main
{
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        //test cases
        //String text="he##l#hel#llo";  //TEST CASE 1: PASS
        //String text="major# spar##ks";  //TEST CASE 2: PASS
        //String text="si###t boy";  //TEST CASE 3: PASS
        //String text="####";  //TEST CASE 4: PASS
        //String text="sit boy A#mit Amlan##i"; //TEST CASE 5: PASS
        //String text="#sit boy A#mit Amlan##i"; //TEST CASE 6: PASS
        //String text="#test";  //TEST CASE 7: FAIL, NOW PASSING
        //String text="#test again A#mit"; //TEST CASE 8: PASS
        //String text="#test again A##mit"; //TEST CASE 9: PASS
        //String text="t#est aga###in A##mit"; //TEST CASE 10: PASS
        //String text="t#est again A##mit"; //TEST CASE 11: PASS
        //String text="te##sting"; //TEST CASE 12: PASS
        //String text="#test a###gain A#mit##"; //TEST CASE 13: PASS
        //String text="s#it boy####";  //TEST CASE 14: PASS
        String text="J#o#h#n"; //UNDOCUMENTED TEST CASE:  PASS
        
        System.out.println("This is final string:" + erase(text));
        System.out.println("This is original string:" + text);
    }
    
    public static String erase(String text)
    {
        int i=0;  //iterate through StringBuilder
        StringBuilder sb = new StringBuilder(text); //places text into
        int indexLastHash;
        int indexFirstHash;
        int count=0;
        
        /*
        while (sb.charAt(0)=='#')
            {
                //removes first character
                sb.delete(0,1);
            }
            */
        
        System.out.println("This is the string to be examined: " + sb);
        try
        {
            do
            {
                //System.out.println("****" + sb.indexOf("#"));
                //System.out.println("****" + sb.lastIndexOf("#"));
                
                System.out.println("value of i: " + i);
                System.out.println("CURRENT TEXT: " + sb);
                //System.out.println("This22 is last index of #:" + sb.lastIndexOf("#"));
                indexLastHash = sb.lastIndexOf("#");
                
                try
                {
                    if (sb.charAt(indexLastHash-1)!='#')
                    {
                        sb.delete(indexLastHash-1, indexLastHash+1);
                        System.out.println("UP HERE1");
                        
                    }
                    
                    //if there is # before the existing one.
                    //it will attack text from other direction.
                    else
                    {
                        System.out.println("UP HERE2");
                        System.out.println(sb);
                        //since it is first hash, there can not be a hash on left hand side
                        indexFirstHash = sb.indexOf("#");
                        System.out.println(sb.indexOf("#"));
                        
                        //need to create situation that it does not leave try block
                        //otherwise code will prematurely end
                        
                        //this ensures as expected.
                        //it performs backspace (first argument)
                        //we know with following, in A#mit
                        //it would remove the A, but it also had to remove the #
                        //String text="#test a##gain A#mit"; // FAILS - double ##
                        //the following notation:
                        //sb.delete(14, 16), so it would include A# and 
                        //exclude m
                        
                        if (indexFirstHash!=0)
                        {
                            sb.delete(indexFirstHash-1, indexFirstHash+1);
                        }
                        
                        //in this scenario, the # would be first index such as
                        //#test.  It would cause exception if it attempts backspace
                        //delete. Instead the # is simply removed.
                        
                        else
                        {
                            sb.delete(indexFirstHash, indexFirstHash+1);
                        }
                        
                        System.out.println("*OUTCOME***: " + sb);
                        
                    }
                    
                    if (sb.indexOf("#")==0)
            {
            do
            {
                System.out.println("INSIDE TRY, REMOVE FIRST #");
                //removes first character
                sb.delete(0,1);
                System.out.println(sb);
            } while((sb.charAt(0)=='#'));
            }
                    
                    
                }
                catch (ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("Reached first char");
                    System.out.println(sb);
                }
                
                i++;
                
            }while (i!=sb.length());
            
            if (sb.indexOf("#")!=-1)
            {
            do
            {
                System.out.println("INSIDE");
                System.out.println(sb);
                //removes first character
                sb.delete(sb.indexOf("#"),sb.indexOf("#")+1);
                count++;
            } while((count<sb.length()));
            }
            
            
        }
        catch (StringIndexOutOfBoundsException e)
        {
            
            System.out.println("Complete");
            System.out.println(sb);
        
            //this is the scenario described in the challenge to remove any #
            //if there are excess # in relation to characters prior to it
            
                    if (sb.indexOf("#")!=-1)
        {
            do
            {
                System.out.println("INSIDE CATCH, REMOVE LEADING #");
                //removes first character
                sb.delete(0,1);
                System.out.println(sb);
            } while((sb.charAt(0)=='#'));
     }

            
            
        
            //This should deal with example 3 and example 4
            
            //System.out.println(sb.charAt(0));
            System.out.println(sb.indexOf("#"));
            
            try
            {
                
            System.out.println("before entering clear # first : " + sb);
            
            
            
            }
            catch (StringIndexOutOfBoundsException s)
            {
                System.out.println("sds");
                return sb.toString();
            }
            
        }
        
        return sb.toString();
        
    }
}