2016-10-15 4 views
0

文字列を受け取り、別の文字列のインスタンスをすべて削除するプログラムを作成しようとしています。たとえば、("Remove them all!", "em")"Rove th all!"と表示されます。しかし、これを実行すると、それは私にjava.lang.StringIndexOutOfBoundsExceptionを与えます。java.lang.StringIndexOutOfBoundsExceptionを受け取り続ける理由

public class LabFive { 

    public static String removeAll(String oldPhrase, String removal){ 
     String newPhrase = ""; 
     for(int i = 0; i <= oldPhrase.length(); i++){ 
      if(oldPhrase.substring(i, (removal.length() + i)) == removal) 
       newPhrase = newPhrase + oldPhrase.substring((removal.length() + 2 + i), oldPhrase.length());   
     } 
     return(newPhrase); 
    } 

    public static void main(String[] args) { 
     System.out.println(removeAll("AaAaAa", "a")); 
    } 
} 
+0

Wあなたは 'replace(" em "、" ")'を使うことはできませんか? –

+0

与えられた答えを見て、あなたを最も援助するものを受け入れる(http://stackoverflow.com/help/accepted-answer)のはどうでしょうか? –

答えて

0

コードにはいくつかの問題があるようです。まず、==を使用して文字列の一致をチェックすることはできません。String.equals()メソッドを使用する必要があります。 Read here

第2に、forループは0からoldPhrase.length()までを繰り返しますが、この長さの値をインデックスに使用しようとすると例外が発生します。 javaでは、文字列は0から始まるインデックスを持つため、インデックスは0から始まり、oldPhrase.length()-1で終了します。

第3に、ロジックが壊れているようです。 substring(int, int)メソッドのパラメータはbeginIndexendIndexです。だから、:

newPhrase = newPhrase + oldPhrase.substring((removal.length() + 2 + i), oldPhrase.length()); 

newPhraseに終わりまでoldPhraseの一部を連結することはないはあなたが望むものをやろうとしています。


これは私のやり方です。アイデアはよりシンプルで、より明確です。私はそれを明確にするためのコメントを追加しました。

テストRepl.it

public static String removeAll(String oldPhrase, String removal) { 

    // if removal is not found return the original string 
    if(oldPhrase.indexOf(removal) == -1) { 
     return oldPhrase; 
    } 

    int removalLength = removal.length(); // storing the length so as not to call .length() again and again 

    for(int i = 0; i < oldPhrase.length(); i++) { // note that <= will cause the exception too 
     int idxOfRemoval = oldPhrase.indexOf(removal); 

     if(idxOfRemoval == i) { // removal is found at the current index, i.e. at index i 
      // take substring from beginning to index of removal + 
      // substring from the end of removal to end of original string 
      oldPhrase = oldPhrase.substring(0, idxOfRemoval) + oldPhrase.substring(idxOfRemoval+removalLength); 
     } 
    } 
    return(oldPhrase); 
} 

public static void main(String[] args) { 
    System.out.println(removeAll("AaAaAa", "a")); 
} 

のライブコード出力:java.lang.StringIndexOutOfBoundsExceptionを説明するための

AAA 
0

最も簡単な方法は、あなたのループである:

for(int i = 0; i <= oldPhrase.length(); i++){...} 

iをしようとしているので、 oldPhrase.length()と等しくなる部分文字列を取得してEM:

oldPhrase.substring(i, (removal.length() + i)) 

ので、あなたは最終的に

oldPhrase.substring(oldPhrase.length(), (removal.length() + oldPhrase.length())) 

文字列の最大インデックスがlength - 1あるので、これは問題であり、あなたがlengthでインデックスにアクセスしようとしているで終わります。

removalが開始され、その後、あなたが返すようにしたい文字列は次のようになります場合は、あなたの文字列を反復することで(あなたが行ったように)だけiで各文字のために、チェックしますremoveAllを行うための強引なやり方

sub(0,i) + removeAll(the rest off your string starting at i+removal.length)

public static String removeAll(String oldPhrase,String removal) { 
    int rem = removal.length(); 
    int n = oldPhrase.length(); 
    // if length of oldPhrase is shorter than removal 
    // then there nothing you need to remove 
    if (n < rem) return oldPhrase; 

    // iterate over your string 
    for (int i = 0; i <= n - rem; i++) { 
     int j; 
     // check if there is a substring, removal, starting at i 
     for (j = 0; j < rem; j++) { 
      if (oldPhrase.charAt(i+j) != removal.charAt(j)) 
       break; 
     } 
     // if there is... 
     if (j == rem) { 
      // return stuff before substring you want to remove + 
      //  removeAll(the stuff after substring you want to remove) 
      return oldPhrase.substring(0,i) + removeAll(oldPhrase.substring(i+rem,n),removal); 
     } 
    } 
    return oldPhrase; 
} 

public static void main(String[] args) { 
    System.out.println(removeAll("AaAaAa", "a")); 
} 

出力:

AAA

関連する問題