Home > Enterprise >  Limit line in the string to 60 characters with newlines
Limit line in the string to 60 characters with newlines

Time:10-19

The program was made to delete double, triple ... spaces from the string and limit characters to 60 per line.

The problem is it gets words into lines that are in the 60th position when the idea is it must skip them.

Output is:

Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems' Java platform.

but the correct output must be:

Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.

My code so far:


      StringBuilder sb = new StringBuilder(text.replaceAll("(^ )|( $)", "").replaceAll("\\s{2,}", " "));
       int i = 0;
       while ((i = sb.indexOf(" ", i   60)) != -1) {
           sb.replace(i,i 1,"\n");
       }
       String result = sb.toString();
       return result;
   }

CodePudding user response:

Here are the results of my 24th test run.

Java was originally developed by James Gosling at Sun Microsystems
(which has since been acquired by Oracle) and released in 1995
as a core component of Sun Microsystems'   Java platform.

Java was originally developed by James Gosling at Sun
Microsystems (which has since been acquired by Oracle) and
released in 1995 as a core component of Sun Microsystems'
Java platform.

The first paragraph is the input. The second paragraph is the output.

Take a look at the code. Do you see several commented out System.out.println statements? That's how you find problems with your code. You write a few lines of code, then you print the intermediate results. Rinse and repeat until your code works.

Here's the complete runnable code.

public class FixedLengthLines {

    public static void main(String[] args) {
        new FixedLengthLines().processString();
    }
    
    public void processString() {
        String text = "Java was originally developed by James Gosling at Sun Microsystems\r\n"   
                "(which has since been acquired by Oracle) and released in 1995\r\n"   
                "as a core component of Sun Microsystems'   Java platform.";
        String temp = text.replaceAll("\\s{2,}", " ");
//      System.out.println(temp);
        
        int lineLength = 60;
        int lineIndex = 0;
        int endIndex = Math.min(lineIndex   lineLength, temp.length());
        StringBuilder builder = new StringBuilder();
        
        while (lineIndex < endIndex) {
//          System.out.println(lineIndex   " "   endIndex   " "   temp.length());
            
            if (endIndex >= lineIndex   lineLength) {
                endIndex = temp.lastIndexOf(" ", endIndex); 
            }
            
            builder.append(temp.substring(lineIndex, endIndex));
            builder.append(System.lineSeparator());
            
//          System.out.println(endIndex   " "   temp.substring(lineIndex, endIndex));
            
            lineIndex = endIndex   1;
            endIndex = Math.min(lineIndex   lineLength, temp.length());
        }
        
        System.out.println(text);
        System.out.println();
        System.out.println(builder.toString());
    }

}

CodePudding user response:

Here you go. After your replace. Not very efficient, but it should to the trick.

    int pos = 0;
    StringBuilder stringBuilder = new StringBuilder(text);
    String finalText = recursiveBuild(stringBuilder,  pos).toString();
    System.out.println(finalText);
}

private static StringBuilder recursiveBuild(StringBuilder stringBuilder, int pos) {
    if (pos 60<stringBuilder.length()){
        if (pos == 0) pos =60;
        int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
        stringBuilder.delete(tempPos, tempPos 1).insert(tempPos, "\n");
        stringBuilder = recursiveBuild(stringBuilder, tempPos 60);
    } else {
        int tempPos = stringBuilder.substring(0, pos).lastIndexOf(" ");
        stringBuilder.delete(tempPos, tempPos 1).insert(tempPos, "\n");
    }
    return stringBuilder;
}

Only works for

        String text = "Java was originally developed by James Gosling at Sun Microsystems (which has since been acquired by Oracle) and released in 1995 as a core component of Sun Microsystems' Java platform.";
  • Related