桌面检查失败:句子在单词中间而不是在空格处被打断

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

这是整个程序的一部分。我遇到的问题是当给定 n 时,程序应该在当前或最后一个 space 处插入一个新行字符以确保字符计数(spaces 不是包含在计数中)在插入新行字符之前永远不会超过 n。它当前的行为是它将用换行符拆分单词。保证:n 永远不会超过最大字的字符数。示例:n = 9,这是个问题。通缉行为:

To be or not
to be that
is the
question

当前行为:

To be or not
 to be th
at is
 the question

如您所见,space 未按预期替换,单词被换行符分隔。我已经多次进行桌面检查,但似乎找不到问题所在。请帮忙!

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();
    }
}

添加第一个换行符后,finLine 中的索引不再匹配 linePart 中的索引。使用 finLine 而不是 linePart 以保持索引一致。

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();
}