/*
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 :)");
        
        String sample = "the quick brown fox jumps over the laxy dog";
        String sample1="ther";
        int lengthLine = 10;
        
        stringExercise  se = new stringExercise(sample, lengthLine);
        
            }
}

class stringExercise
{
    private String sample;
    private int lengthLine;
    double stringsRequired;
    
    public stringExercise(String sample, int lengthLine)
    {
        this.sample=sample;
        this.lengthLine=lengthLine;
        
        checkString();
        
    }
    
    public String checkString()
    {
        int array=0;
        
        //no way to break the text;
        if ((sample.length()>lengthLine) &&  (sample.indexOf(" ")==-1))
            {
                return null;
                
            }
        
        //if the string is less than or equal segment specified, it will return entire string     
        if (sample.length()<=lengthLine)
        {
            return sample;
            
        }
        
        //if the segment is greater or equal to segment and there is a space in text....
        if ((sample.length()>=lengthLine) &&  (sample.indexOf(" ")!=-1))
            {
                System.out.println("should reach here");
                
                //this is perhaps wrong since it will truncate words....
                stringsRequired=Math.ceil(sample.length()/lengthLine);
                //
                System.out.println("length of sample: " + sample.length());
                //System.out.println("number strings required: " + stringsRequired);
                System.out.println("length of strings: " + lengthLine);
                
                
                //int stringsRequiredConverted = (int) stringsRequired;
                
                // for now it is setting array of 50 strings, but a better measure will be used soon
                
                
                String output[] =  new String[50];
                
                int init=0;
                
                do
                {
                
                
                // this is going through all characters and checking lengthline-1 element
                for (int i=0; i<sample.length(); i=i+(lengthLine-1))
                {
                    
                    System.out.println("\nvalue of i: " + i);
                    System.out.println("current array is:" + array);
                    
                    // this bit is needed since it will otherwise skip the first character in the nested loop
                    if (array==0)
                    {
                        init=0;
                    }
                    
                    else
                    {
                        init=1;
                    }
                    
                    // it needs to check also that there is a space on last position of each segment or there is no space
                    //at start of next segment
                    
                    
                    for (int k=0; k<lengthLine; k++)
                    {
                        System.out.println("value of K: " + sample.charAt(i+k));
                        
                        //if (sample.charAt(i+k)==' ')
                        //{
                          //  k++;
                    //    }
                        
                        // this requires trickier part of code...
                        // need something to ensure that a word is not truncated.
                        // this means that last character can be a space
                        // or the character after last can be a space
                        // but neither 
                        
                        // logic needs to keep length of each word and then create 
                        
                        if (k=lengthLine-1)
                        {
                            if (   (sample.charAt(i+k)==' ' && sample.charAt(i+k)!=' ')  || (sample.charAt(i+k)!=' ' && sample.charAt(i+k)==' ')  )                     )
                            {
                                System.out.println("correct break up of segments");
                            }
                            
                        }
                        
                        
                        output[array]=output[array] + sample.charAt(i+k);
                        
                        
                    
                     }
                    
                    array++;
                }
            } while (array<50);
                
                
            }
        return null;
    }
}