/*
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 :)");
        
        wordProcessor();
}


    public static void wordProcessor()
    {
        String test="This45 is an2 example of a short essay87766666666666666666 and it will8 be99 formatted as per instructions amit amlani of ";
        
        // This example should not need a StringBuffer or equivalent since there will be no requirement to modify existing String
        
        StringTokenizer st = new StringTokenizer (test);
        int count=0;
        String temp="";
        String convertedTokenString="";
        
        String beforeAddingWord;
        Boolean incorrectWordLength=false;
        Boolean acceptableCharacter=false;
        int characterLineLimit=80;
        int processedCharacters=0;
        int lineCount=0;
        int numberLines;
        int essayLine=0;
        
        System.out.println(test.length());
        System.out.println(characterLineLimit);
        
        double approxLineCount = Math.ceil(((double)test.length()/characterLineLimit));
        lineCount = (int) approxLineCount;
        System.out.println("ss  " + approxLineCount);
        
        StringBuffer sb = new StringBuffer();
        
        
       // List <StringJoiner> essay = new ArrayList<>();
        
        
        
        /*
        // this pre-populates essay with lines... not possible to perform this in main code with existing logic
        for (int m=0; m<lineCount; m++)
        {
           essay.add(new StringJoiner(" "));
        }
        */
        
        
        String [] essay = new String[lineCount];  // there is problem here in determing exact number of line for array
        // it is going to be based on length essay chars / line limit.
        // although spaces are not relevant to line length as per specifications.
        // can potentially use recursion here to count number of spaces.... or complete entire exercise
        // it was attempted to declare this array later on in the code once exact number lines was determined, 
        // however it was local to the hasMoreTokens(), so not possible to reach it further in execution 
        // int lineCount over compensates which is better since it will prevent outbound exception...
        
        
        //stringJoiner ff = new stringJoiner(" ");
        StringJoiner sj = new StringJoiner(" ");
        
        char [] lowerCase = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        char [] upperCase = new char[]{'A','C','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        
        while (st.hasMoreTokens())
        {
            //st.nextToken();
            temp=st.nextToken();
            
            incorrectWordLength=false;
            
            if (temp.length()<1  || temp.length()>15)     // if word in accepted limits
            {
                System.out.println("Word is longer than 15 characters: " + temp);
                incorrectWordLength=true;
            }
            
            count++;
            
            if (!incorrectWordLength)     // if the word is not wrong length
            {
                System.out.println(count);
                
            
            convertedTokenString=temp.toString();
            
            for (int i=0; i<convertedTokenString.length();i++)
            {
                acceptableCharacter=false;
                processedCharacters = processedCharacters + convertedTokenString.length();
                
                for (int j=0;j<lowerCase.length;j++)
                {
                    if (convertedTokenString.charAt(i)==lowerCase[j] || convertedTokenString.charAt(i)==upperCase[j])
                    {
                        acceptableCharacter=true;
                    }
                }
                
                if (!acceptableCharacter)
                {
                    System.out.println("The following word has incorrect character: " + convertedTokenString);
                    break;
                    
                }
                
            }
            
            
            beforeAddingWord = sj.toString();
            
            //this excludes spaces since it is processing character count from tokens
            //numberLines = (int) processedCharacters/characterLineLimit;
            
            System.out.println("The current string: " + beforeAddingWord);
            
            
            // However the line limit should not be constrained by number existing spaces
            // For 3 words there are two spaces in stringjoiner, so for count words there are   (count -1) spaces
            
            if ((beforeAddingWord.length() + convertedTokenString.length() + (count-1)) <=characterLineLimit)
            {
                //sb.replace(0,sb.length(), "");
                //sb.append(convertedTokenString);
                
                //essay.add(convertedTokenString);
                
                sj.add(convertedTokenString);
                
                // essay.add(sb);
                
                // System.out.println("Extended string: " + sj);
                // Now it will add the stringjoiner into the lines Array
                
                
                while (!st.hasMoreTokens())
        {
            essay[essayLine] = sj.toString();
            break;
        }
                
            }
            
            else
            {
                System.out.println("How many times: " + convertedTokenString);
                essay[essayLine] = sj.toString();
                
                sj= new StringJoiner(" ");
                sj.add(convertedTokenString);
                
                essayLine++;
                
                //System.out.println("content: " + sj);
                
                //essay.add(sb);
                //sb.replace(0,sb.length(), "");
                
                //it will remove content in the stringjoiner... and start with a a newly added token
                //sj.delete();
                //essay.add(new StringJoiner(""));
                //sb.append(sj.toString());
                
                //essayLine++;
                //essay[essayLine].add(convertedTokenString);
                
                //dd[lineCount]=sj.toString();
                //lineCount++;
            }
            
            
            
            
            //if convertedTokenString
        }
            
        }
        System.out.println("Number words: " + count);
        //System.out.println("Current line: " + sj.toString());
        
        //Iterator it = essay.iterator();
        
        
        for (String s: essay)
        {
            System.out.println(s);
            
        }
        /*
        while (it.hasNext())
        {
            Object finalLine = it.next();
            System.out.println("dsds:" + finalLine);
        }
        */
        
        /*
        for (String m: dd)
        {
            System.out.println(m);
        }
        r
        */
        
    }
}
        
    

