
/*
Online Java - IDE, Code Editor, Compiler

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

// why do we need stringjoiner, stringbuilder        and tokenizer  (split words)  

import java.util.*;

public class Main
{
    public static void main(String[] args) {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        
        String text = "The quick brown fox jumps over the lazy dog";
        
        int count=0;
        int k=16;
        String temp;
        //String sentence;
        String tokentoString;
        String line;
        int wordCount=0;
        int lineLength;
        String lineBeforeLastWord;
        int lengthLineBeforeLastWord;
        String completeLine;
        String totalExtraPaddingBetweenWords;
        int extraPaddingBetweenWords=0;
        String temp1;
        
        String delimiter=" ";
        
        totalExtraPaddingBetweenWords=delimiter;
        
        String sentence="";
        StringJoiner sj = new StringJoiner(delimiter);
        StringJoiner sj1;
        StringJoiner sj2;
        
        StringTokenizer st = new StringTokenizer(text, delimiter);
        StringTokenizer st1;
        StringBuilder sb1 = new StringBuilder();
        
        while (st.hasMoreTokens())
        {
            wordCount++;
            
            temp=st.nextToken();
            tokentoString = temp.toString();
            
            StringBuilder sb = new StringBuilder(tokentoString);
            
            lineBeforeLastWord = sj.toString();
            
            sj.add(sb);
            line = sj.toString();
            lineLength=line.length();
            
            
            System.out.println("your sentence getting bigger:" + line);
            
            System.out.println("current length: " + lineLength);
            System.out.println("number words: " + wordCount);
            
            //need to think about how to allocate spaces...
            //need to keep a count of the words
            
            //first thing is if the natural line has taken it over k,
            // need to truncate a word.....
            //count length of words...
            // subtract it from k....
            //if less than (wordCount-1)
            //pads at front....    (COMPLETE...)
            
            if (lineLength>k)
            {
                //need remove last token....
                //can not remember how I did that last time..remember
                
                //kept copy of stringjoiner...
                
                lengthLineBeforeLastWord= lineBeforeLastWord.length();
                
                System.out.println("rolled back:" + lineBeforeLastWord);
                System.out.println("Length rolled back line: " + lengthLineBeforeLastWord);
                
                //at this point there clearly is not enough buffer to allow an additional space between the characters...
                //it has to therefore force the spaces on the left hand side...
                
                
                int buffer = k-lengthLineBeforeLastWord;
                
                System.out.println("The current buffer is: " + buffer);
                System.out.println("Number words to accomodate for: " + (wordCount-1));
                
                //The buffer (1) is less than number words (3)
                
                //note that for every 3 words, 2 spaces are required....
                if (k-lengthLineBeforeLastWord<wordCount-1)
                {
                
                
                // if extra spaces can go between words (such as two between words),
                //this is preferable than adding padding at front... as per requirements....
                //it is only possible if there is no remainder, so modulus will be taken...
                
                
                if (buffer%(wordCount-1)==0)
                {
                    extraPaddingBetweenWords = buffer%(wordCount-1);
                
                
                
                        //***************** need to think which loop is this going in.... ****************
                        // it can be nested in one above, but not required....
                        
                  for (int j=0; j< extraPaddingBetweenWords;j++)
                  {
                      totalExtraPaddingBetweenWords = totalExtraPaddingBetweenWords + " ";
                  }
                    //the only way to get extra spaces between the existing lineBeforeLastWord
                    // is to create another StringTokenizer with same delimiter ""
                    //Then to set up another StringJoiner with new delimiter as below...
                    
                    st1=new StringTokenizer(lineBeforeLastWord, "");
                    
                    sj2 = new StringJoiner(totalExtraPaddingBetweenWords);
                    
                    while (st1.hasMoreTokens())
                    {
                        //we know that all tokens will be part of the same line...
                        
                        temp1=st1.nextToken();
                        sj2.add(temp1);
                    }
                 
                } 
                    
                
                //*************************
                
                else
                {
                
                //all spaces appended at front to fill the buffer
                
                sb1.append(lineBeforeLastWord);
                
                for (int b=0; b<buffer; b++)
                {
                    count++;
                    sb1.insert(0," ");
                    System.out.println(count + " padding added at front");
                    
                }
                
                completeLine = sb1.toString();
                System.out.println("First line completed:" + completeLine);
                
                //at this point, it needs to wipe out all the contents in the stringjoiner
                // it also needs to keep temp back into it since this was last word that was 
                //discarded.
                sj.add(temp);
               
                }
                
                //reset all StringJoiners, delete all StringBuilders...
                 sj=new StringJoiner(" ");
                 sj1=new StringJoiner(" ");
                 sj2=new StringJoiner(" ");
                
                //it also has to remove contents of both StringBuilders....
                sb1.delete(0,sb1.length());
                sb.delete(0,sb.length());
                
                
                
                
                
                }
                
                
                
                
            }
            
            
        }
        
        
        
    }
    
}
