Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 桌面检查失败:句子在单词中间被打断,而不是空格_Java_Newline_Stringbuilder_Desk Check - Fatal编程技术网

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

Java 桌面检查失败:句子在单词中间被打断,而不是空格,java,newline,stringbuilder,desk-check,Java,Newline,Stringbuilder,Desk Check,这是整个计划的一部分。我遇到的问题是,当给定n时,程序应该在当前或最后一个空格插入新行字符,以确保在插入新行字符之前,字符计数空格不包括在count中,且不会超过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 正如您所看到的,空格没有像预期

这是整个计划的一部分。我遇到的问题是,当给定n时,程序应该在当前或最后一个空格插入新行字符,以确保在插入新行字符之前,字符计数空格不包括在count中,且不会超过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
正如您所看到的,空格没有像预期的那样被替换,单词被新行字符打断。我已经检查了好几次了,但似乎找不到问题。请帮忙

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

新的行为现在是在第一行之后的每一行前面加一个空格,在问题前面加一个换行符。谢谢你的帮助!!编辑后用换行符替换空格,而不仅仅是在空格前插入换行符。先生,你的岩石等级是天文数字!