1

私のCompSciクラスでは、Would You Right?を作成しています。チャットボット プロジェクトの関数。このString.split()方法はこれでうまく機能しますが、それなしで実行できればボーナスポイントを獲得できます. レプリケートするメソッドを作成するだけで、これを行うことにしましたString.split

private String[] separate (String phrase, String omit1, String omit2)
{
    int c = 0;

    //gets rid of leading and trailing whitespace, replaces target characters
    //with the # character
    phrase = phrase.trim();
    phrase = phrase.replace(omit1, "#");
    phrase = phrase.replace(omit2, "#");

    //detects the number of phrases to be included in the array
    for (int i = 0; i < phrase.length(); i++)
        if (phrase.charAt(i) == '#')
            c++;

    //creates array list based on number of phrases
    String[] phraseList = new String[c];
    c = 0;

    //builds phrases from characters found between occurrences
    //of the # character
    for (int i = 0; i < phrase.length(); i++)
    {
        if (phrase.charAt(i) == '#')
            c++;
        else if (phrase.charAt(i) != '#')
            phraseList[c] += phrase.charAt(i);
    }

    return phraseList;

}

「お茶を飲むか、クッキーを食べるか、腕立て伏せをするか」というフレーズでこの方法を使用するときはいつでも。(omit1 は "," であり、omit2 は "or" である)、次の例外がスローされます。

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Magpie.separate(Magpie.java:306)
    at Magpie.getResponse(Magpie.java:44)
    at MagpieRunner.main(MagpieRunner.java:24)

これは配列のカウンターと関係があることを認識していますが、phraseListこれを修正しようとしてもこれまでのところ役に立ちませんでした。

ヘルプはありますか?

4

5 に答える 5

1

split()他の回答でコードの何が問題なのかがわかったので、実際のメソッドのように動作する、文字列を分離するよりクリーンな方法を次に示します。

private String[] separate(String phrase, String delim) {
    List<String> tokens = new ArrayList<String>();

    // add delimiter to the end of the string
    // so last token will be included properly
    phrase += delim;

    // start from index of first deliminator
    // i is the index for the deliminator
    // j is the index for the first char of the expression before deliminator
    int i, j = 0;

    // while there are deliminators
    while( (i = phrase.indexOf(delim, j)) != -1)  {
        // obtain the current token from j to deliminator location
        String token = phrase.substring(j, i);
        // trim leading/trailing spaces of the token and make sure it has any chars
        // if it does, add the token to list
        if(token.trim().length() != 0) {
            tokens.add(token);
        }
        // update j to the first character after the deliminator
        j = i + delim.length();
    }

    return tokens.toArray(new String[0]);
}
于 2014-09-16T16:40:17.293 に答える
1

1つでも # ある場合、2つの文字列があるため、新しい配列を作成するときに c+1 を実行する必要があります

お気に入り

//creates array list based on number of phrases
String[] phraseList = new String[c+1];
c = 0;

&replaceAll(omit1,"#")ではなくandを使用する必要がありますreplace(omit1,"#")replace(omit2,"#")

null がどこに来ているかについて、より多くの情報を提供できますか?

編集:

次のようなことを試しましたか?

phraseList[0]="";
for(int i = 0; i < phrase.length(); i++)
  {
     if(phrase.charAt(i) == '#')
     {
        c++;
        phraseList[c]="";
     }else if(phrase.charAt(i) != '#')
     {
        phraseList[c] += phrase.charAt(i);
     }
  }
于 2014-09-16T16:32:29.887 に答える
0

StringTokenizer を使用しないのはなぜですか? (以下は Java doc の例です)

以下は、トークナイザーの使用例の 1 つです。コード:

 StringTokenizer st = new StringTokenizer("this is a test");
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

次の出力を出力します。

 this
 is
 a
 test
于 2014-09-16T16:48:14.293 に答える
0

配列は 0 から始まるインデックスが付けられますが、配列の長さは初期値 1 で決定されます。

したがってc、フレーズの数が表示されますが、実際には配列内のインデックスの数 ( c0 から始まるため) であり、実際の長さではありません。実際の長さは c + 1 になります (長さは 1 から計算されるため)

Index     0 | 1 | 2 | 3 | 4
Length    1 | 2 | 3 | 4 | 5

たとえば、c = 4 (インデックス = 4) の場合、String[] の長さは 4 になりますが、本来は 5 です。これが、その ArrayIndexOutOfBounds をスローするものです。お役に立てれば :)

于 2014-09-16T16:50:52.707 に答える