2016-10-22 13 views
1

メソッドdeleteCharAt(0)を使用して、位置0の文字を削除しようとしていて、その文字を末尾に追加しようとしています。文字は最後に追加されますが、メソッドdeleteCharAt(0)は実行されません。なぜそれが動作していないのかは分かりません。StringBuilderメソッドdeleteCharAt文字を削除しない

Input: Test test test 
    Expected output: esttqw esttqw esttqw 
    Actual output: ttqw testtqw testtqw 

以下は私のコードです。どうもありがとう。

pT = pT.toLowerCase(); //converts the string to lower case 

    String[] strArr = pT.split(" "); //splits the string into an array 


    for(String subStr : strArr){ //for each substring in the string array 

     char first = subStr.charAt(0); 
     stringBuilder.append(subStr); //converts the string to a stringbuilder object 

     if((first=='a') || (first == 'e') || (first == 'i') || (first == 'o') || (first == 'u')){ //starts with a vowel 
      stringBuilder.append((char)charRand1); //appends y1 to the end of the string 
      stringBuilder.append((char)alphaRand3); //appends x3 to the end of the string 
      stringBuilder.append((char)alphaRand4); //appends x4 to the end of the string 
      stringBuilder.append(" "); 
      encryptedSS = stringBuilder.toString(); //converts stringbuilder back to string 
     } 
     else{ //starts with a consonant 
      stringBuilder.deleteCharAt(0); //deletes the first character 
      stringBuilder.append(first); //appends the first character to the end of the word 
      stringBuilder.append((char)alphaRand1); //append x1 to the end of the word 
      stringBuilder.append((char)alphaRand2); //append x2 to the end of the word*/ 
      stringBuilder.append(" "); 

      encryptedSS = stringBuilder.toString(); //converts string builder back to an array 
     } 

    } 
+0

入力例、それに得られると思われる出力、実際の出力などを教えてください。 – Mureinik

+0

私はちょうどいくつかを追加しました。ご協力ありがとうございました。 –

答えて

0

あなたがstringBuilderを初期化するところあなたのスニペットが表示されませんが、あなたはループの外で、一度それをやっているかのように思えます。このため、deleteCharAt(0)を呼び出すと、現在処理中の文字列ではなく、結果全体の最初の文字が削除されます。これを避けるために、あなたが処理する文字列あたりStringBuilder一時的に作成することができます。

for(String subStr : strArr) { 
    // New StringBuilder per String 
    StringBuilder stringBuilder = new StringBuilder(subStr); 

    char first = subStr.charAt(0); 

    if ((first=='a') || (first == 'e') || (first == 'i') || (first == 'o') || (first == 'u')){ //starts with a vowel 
     stringBuilder.append((char)charRand1); //appends y1 to the end of the string 
     stringBuilder.append((char)alphaRand3); //appends x3 to the end of the string 
     stringBuilder.append((char)alphaRand4); //appends x4 to the end of the string 
     stringBuilder.append(" "); 
     encryptedSS = stringBuilder.toString(); //converts stringbuilder back to string 
    } 
    else{ //starts with a consonant 
     stringBuilder.deleteCharAt(0); //deletes the first character 
     stringBuilder.append(first); //appends the first character to the end of the word 
     stringBuilder.append((char)alphaRand1); //append x1 to the end of the word 
     stringBuilder.append((char)alphaRand2); //append x2 to the end of the word*/ 
     stringBuilder.append(" "); 

     encryptedSS = stringBuilder.toString(); //converts string builder back to an array 
    } 
+0

私はそれを試みましたが、今は私の文字列全体が削除されているように見えます。 –

+0

私はそれを試みましたが、今は私の文字列全体が削除されているように見え、ちょうど "esttqw"これは正しいですが、文字列の残りの部分が必要です)、 "esttqw esttqw esttqw"の代わりに。もう一度あなたに感謝します。 –

+0

@ Bastard24あなたは 'encryptedSS'インスタンスを何とか完全な文字列に蓄積する必要があります。元の質問に掲載されたコードでこれをどうやって行ったのかははっきりしていませんが、何らかの理由でそれをやっていないのならば、あなたはすべきです。 – Mureinik

0

私はすべてのサブストリングを組み立てるために、別のStringBuilderオブジェクトを追加する必要がありました。

pT = pT.toLowerCase(); //converts the string to lower case 

    String[] strArr = pT.split(" "); //splits the string into an array 


    for(String subStr : strArr){ //for each substring in the string array 
     char first = subStr.charAt(0); 

     StringBuilder stringBuilder = new StringBuilder(); 
     stringBuilder.append(subStr); 

     if((first=='a') || (first == 'e') || (first == 'i') || (first == 'o') || (first == 'u')){ //starts with a vowel 
      stringBuilder.append((char)charRand1); //appends y1 to the end of the string 
      stringBuilder.append((char)alphaRand3); //appends x3 to the end of the string 
      stringBuilder.append((char)alphaRand4); //appends x4 to the end of the string 
      stringBuilder.append(" "); 
      encryptedSS = stringBuilder.toString(); //converts stringbuilder back to string 
     } 
     else{ //starts with a consonant 
      stringBuilder.deleteCharAt(0); //deletes the first character 
      stringBuilder.append(first); //appends the first character to the end of the word 
      stringBuilder.append((char)alphaRand1); //append x1 to the end of the word 
      stringBuilder.append((char)alphaRand2); //append x2 to the end of the word*/ 
      stringBuilder.append(" "); 

      encryptedSS = stringBuilder.toString(); //converts string builder back to an array 
     } 

     builder2.append(encryptedSS); //appends the encrypted substring to the stringbuilder 
    } 
関連する問題