/*
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(100, 80, "This45 is an2 example of a short essay87766666666666666666 and it will8 be99 formatted as per instructions amit amlani ofdsf3224 434343 22233");
    }
    
    //Parameter 1 is word limit
    //Parameter 2 is character line limit
    //Parameter 3 is essay
    
    public static void wordProcessor(int wordLimit, int characterLineLimit, String essay)
    {
        StringTokenizer st = new StringTokenizer (essay);
        int count=0;
        String temp="";
        String convertedTokenString="";
        String beforeAddingWord;
        Boolean incorrectWordLength=false;
        Boolean acceptableCharacter=false;
        int processedCharacters=0;
        int lineCount=0;
        int numberLines;
        int essayLine=0;
        int minWordLength=1;
        int maxWordLength=15;
        
        System.out.println("\nThis is the essay: \n" + essay);
        double approxLineCount = Math.ceil(((double)essay.length()/characterLineLimit));
        lineCount = (int) approxLineCount;
        
        System.out.println("\nApproximate number of lines within constraints: " + lineCount +
        "\n" + "Character line limit: " + characterLineLimit + "\n" + "Word length (characters): " +
        minWordLength+ " - " + maxWordLength);
        System.out.println("\n***Analysis***");
        
        StringBuffer sb = new StringBuffer();
        String [] finalEssay = new String[lineCount];
        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())
        {
            temp=st.nextToken();
            convertedTokenString=temp.toString();
            processedCharacters = processedCharacters + convertedTokenString.length();
            incorrectWordLength=false;
            
            if (temp.length()<minWordLength || temp.length()>maxWordLength)
            {
                System.out.println("Word is longer than 15 characters: " + temp);
                incorrectWordLength=true;
            }
            
            count++;
            
            if (count>wordLimit)
            {
                System.out.println("******ESSAY EXCEEDED " + wordLimit + "***");
            }
            
            if (!incorrectWordLength)
            {
                for (int i=0; i<convertedTokenString.length();i++)
                {
                    acceptableCharacter=false;
                    
                    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();
                
                if ((beforeAddingWord.length() + convertedTokenString.length() + (count-1))<=characterLineLimit)
                {
                    sj.add(convertedTokenString);
                    
                    while (!st.hasMoreTokens())
                    {
                        finalEssay[essayLine] = sj.toString();
                        break;
                    }
                }
                else
                {
                    finalEssay[essayLine] = sj.toString();
                    sj= new StringJoiner(" ");
                    sj.add(convertedTokenString);
                    essayLine++;
                }
            }
        }
        System.out.println("\nWord count: " + count+"\n");
        System.out.println("\nCharacter count (with spaces) Word count: " + (processedCharacters+(count-1)));
        System.out.println("\nCharacter count (without spaces) Word count: " + (processedCharacters)+"\n");
        
        for (String s: finalEssay)
        {
            System.out.println(s);
        }
    }
}