Search code examples
javanewlinestringbuilderdesk-check

Desk check fail: Sentences broken up in middle of words and not on spaces


This is part of a whole program. The problem I'm having issues with is when given n, the program is supposed to insert a new line character at the current or last space to make sure that the character count (spaces aren't included in count) never exceeds n before a new line character is inserted. Its current behavior is that it will split words with new line characters. Guarantees: n will never exceed the char count of largest word. Example: n = 9, To be or not to be that is the question. Wanted behavior:

To be or not
to be that
is the
question

Current behavior:

To be or not
 to be th
at is
 the question

As you can see, the spaces aren't replaced like intended and words are broken by new line character. I've desk-checked multiple times, and I can't seem to find the problem. Please help!

public class Separator {

    int n; //the int in front of read line
    int cCount = 0;  //character counter
    int i;
    int lastSpc;
    char c;  //for character iterator
    String linePart;

    public String separate(int n, String linePart) {
        StringBuilder finLine = new StringBuilder(linePart);

        for (i = 0; i < linePart.length(); i++) {  //inspects each character in string.
            c = linePart.charAt(i);

            if (c == ' ') {                          //determines whether or not char is a space
                if (cCount == n ) {                  //checks char count
                    finLine.replace(i, i, System.lineSeparator());   //adds new line if char count is reached right before space.
                    cCount = 0;
                    lastSpc = i;
                }
                else {
                    lastSpc = i;                     //assigns index to variable so no need to reverse through string.
                }
            }
            else {
                cCount++;
                if (cCount == n) {                      //if char count is reached while inspecting letter, 
                    finLine.replace(lastSpc, lastSpc, System.lineSeparator());      //replaces last space with new line char
                    cCount = i - lastSpc;
                }
            }
        }
        return finLine.toString();
    }
}

Solution

  • Indexes into finLine no longer match indexes into linePart after the first newline is added. Use finLine instead of linePart to keep the indexing consistant.

    public String separate(int n, String linePart) {
        StringBuilder finLine = new StringBuilder(linePart);
        int lines_added = 0;
        for (i = 0; i < finLine.length(); i++) {  //inspects each character in string.
            c = finLine.charAt(i);
    
            if (c == ' ') {                          //determines whether or not char is a space
                if (cCount == n ) {                  //checks char count
                    finLine.replace(i, i+1, System.lineSeparator());   //adds new line if char count is reached right before space.
                    cCount = 0;
                    lastSpc = i ;
                }
                else {
                    lastSpc = i;                     //assigns index to variable so no need to reverse through string.
                }
            }
            else {
                cCount++;
                if (cCount == n) {                      //if char count is reached while inspecting letter, 
                    finLine.replace(lastSpc, lastSpc+1, System.lineSeparator());      //replaces last space with new line char
                    cCount = i - lastSpc;
                }
            }
        }
        return finLine.toString();
    }