/*
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
        //String text = "This #i#s# a v###r#y lo#n##g###sentence t#o# s#e#e# t#h### ##c#o#d#e#.";
        String text="   ###";
        
        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
        
        //places text into StringBuilder in order to manipulate (delete...)
        StringBuilder sb = new StringBuilder(text);
        
        int indexLastHash;  //used in conjunction with lastIndexOf("#")
        int indexFirstHash; //used in conjunction with IndexOf("#")
        int count=0; //as final part
        
        System.out.println("This is the string to be examined: " + sb);
        
        //the try and catch is required (StringIndexOutOfBoundsException) 
        //due to the nature of the code
        //indexLastHash-1 is below 0 in the following line, 
        //if (sb.charAt(indexLastHash-1)!='#')
        
        try
        {
            do
            {
                //System.out.println("value of i: " + i);
                indexLastHash = sb.lastIndexOf("#");
                indexFirstHash=sb.indexOf("#");
                
                //This try and catch block were constantly used during testing
                //since several situations violated ArrayIndexOutOfBoundsException
                //I was able to shift code into the catch statement..
                //However once testing had finished, no code had entered the catch statement.
                //I will mantain this for now for troubleshooting purpose should it occur
                try
                {
                    //if previous character is not a backspace key pressed
                    if (sb.charAt(indexLastHash-1)!='#')
                    {
                        System.out.println("Deleting character: " + sb.charAt(indexLastHash-1) + " at index: " + (indexLastHash-1));
                        //it will delete previous character and also the backspace key
                        sb.delete(indexLastHash-1, indexLastHash+1);
                        System.out.println("Formatted text: " + sb);
                    }
                    
                    //if there is # before the existing one.
                    //it will attack text from other direction.
                    else
                    {
                       
                        //since it is first hash, there can not be a hash on left hand side
                        indexFirstHash = 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)
                        {
                            System.out.println("Deleting character: " + sb.charAt(indexFirstHash-1) + " at index: " + (indexFirstHash-1));
                            sb.delete(indexFirstHash-1, indexFirstHash+1);
                            System.out.println("Formatted text: " + sb);
                        }
                        
                        //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
                        {
                            System.out.println("Removing ineffective BACKSPACE at index: " + indexFirstHash);
                            sb.delete(indexFirstHash, indexFirstHash+1);
                            System.out.println("Formatted text: " + sb);
                        }
                        
                    } //end of else statement
                    
                    //if there is a BACKSPACE at first index in StringBuilder
                    if (sb.indexOf("#")==0)
                    {
                        do
                        {
                            System.out.println("Removing ineffective BACKSPACE at index 0");
                            sb.delete(0,1);
                            System.out.println("Formatted text: " + sb);
                            
                        }while((sb.charAt(0)=='#'));
                    }
                    
                } //end try
                
                catch (ArrayIndexOutOfBoundsException e)
                {
                    System.out.println("SHOULD NOT REACH HERE");
                }
                
                i++;  // counter for indexing
                
            }while (i!=sb.length());
            
            //This is most thorough sweep, it will remove ineffective
            //# at front and end.
            //Those intermittent ones should have been executed in all scenarios
            //at this point
            if (sb.indexOf("#")!=-1)
            {
                do
                {
                    System.out.println("Removing ineffective BACKSPACE at index: " + sb.indexOf("#"));
                    //removes single character
                    
                    if (sb.indexOf("#")==0)
                    {
                        sb.delete(sb.indexOf("#"),sb.indexOf("#")+1);
                    }
                    else
                    {
                        //since not backspace at index 0, it has to do normal operation.
                        sb.delete((sb.indexOf("#")-1), (sb.indexOf("#")+1));
                        
                    }
                    
                    
                    System.out.println("Formatted text: " + sb);
                    count++;
                
                } while((count<sb.length()));
            }
            
        }  //end of main try block
        catch (StringIndexOutOfBoundsException e)
        {
            //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("Removing ineffective BACKSPACE at index 0");
                    sb.delete(0,1);
                    System.out.println("Formatted text: " + sb);
                
                //as it removes leading #, it would make way for any forthcoming
                //# (if applicable)
                } while((sb.charAt(0)=='#'));
            }
            
             return sb.toString();
            
        }  //end of catch
        
        return sb.toString();
    
    }
}