0

これはプログラム全体の一部です。私が抱えている問題は、n が指定された場合、プログラムは現在または最後のスペースに改行文字を挿入して、文字数 (スペースはカウントに含まれません) が 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();
    }
}
4

1 に答える 1

0

最初の改行が追加された後、finLine へのインデックスは、linePart へのインデックスと一致しなくなりました。インデックスの一貫性を保つには、linePart の代わりに finLine を使用します。

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();
}
于 2015-09-21T22:01:34.877 に答える