/*
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 :)");
        
        checksentence cs;
        
        String delim=" ";
        String sentence = "My name is Amit Amlani";
        String [] output = new String[50];  //assumes 50 words#
    
        int i=0;
        int k=10;
        
        
        StringTokenizer st = new StringTokenizer (sentence, delim);
        
        //checking tokens in original string. Delimiter is single whitespace
        while (st.hasMoreTokens())
        {
            
            output[i]= st.nextToken().toString();   //converted token into a String
            System.out.println(output[i]);
            
            i++;
            
        }
        
        //output is the array of the string tokens   
        //k is the limit
        //sentence is the original sentence
        cs = new checksentence(output, k, sentence);  
    }
}

class checksentence
{
    String [] output;
    int limit;
    int runningLength;
    
    //this will keep creating the sentence, need to use something that is immutable in order to remove characters....
    // not mutable
    StringBuffer ff = new StringBuffer ();
    String sentence;
    StringJoiner temp;
    
    
    public checksentence(String [] output, int limit, String sentence)
    {
        this.output=output;
        this.limit=limit;
        this.sentence=sentence;
        
        checklength();
    }
    
    
    public void checklength()
    {
        //all the words are stored in tokens.
        //need to check length of each token...
        //stop if the count is greater than limit
        StringJoiner sj = new StringJoiner (" ");
       
        
        String [] finalOutput = new String[50];  // this is a guess of number lines. it can never be calculated in advance
        
        for (int i=0; i<output.length;i++)
        {
            runningLength = output[i].length() + runningLength;
            
            temp=sj;  // this keeps the old string in the case that it needs to roll back
            
            sj.add(output[i]); // this adds the next word and whitespace will be included
            
            //this will then ignore the current word if it exceeeds the limit
            
            if (runningLength>limit)
            {
                sj=temp;  // this restores the sentence without word taking it over limit
                //count=count-1;
                i=i-1;   // this ensures on the next iteration, it will start with word which has taken over limit
            }
            
            runningLength=0;
            
            //now it can finally add the string to a string array as per requirements...
            
            //sj.add("\");
            
            finalOutput[i]= '"' + sj.toString() + '"';
        }
        
        //now it will output the Strings. But beforehand, it has to be converted into the following
        //format of a comma instead of a whitespace
        
        for (String k: finalOutput)
        {
            System.out.println(k);
       
        }
    
    }
}
