1

I'm attempting to delete the character at position 0 using the method deleteCharAt(0), and append that character (already copied) to the end. The character will append to the end, but the method deleteCharAt(0) is not executing. I can't really tell why it's not working.

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

Below is my code. Many thanks.

    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
        }

    }

2 Answers2

0

Your snippet doesn't show where you initialize stringBuilder, but it seems as though you're doing it once, outside the loop. Because of this, calling deleteCharAt(0) just removes the first character of the entire result, not the string you're currently processing. In order to avoid this you could create a temporary StringBuilder per String you process:

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
    }
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I attempted that but it seems as though now my entire string is being deleted and I'm left with: –  Oct 22 '16 at 22:23
  • I attempted that but it seems as though now my entire string is being deleted and I'm left with just "esttqw" (which is correct, but just need the rest of the string), instead of "esttqw esttqw esttqw". Once again thank you. –  Oct 22 '16 at 22:26
  • @Bastard24 You need to somehow accumulate the `encryptedSS` instances to the complete string. It's unclear how you did this in the code posted in the original question, but if you aren't doing that for some reason - you should. – Mureinik Oct 22 '16 at 22:34
  • I added another StringBuilder object to accumulate the encryptedSS instances. Many thanks for your help! –  Oct 22 '16 at 22:38
0

I needed to add another StringBuilder object to assemble all the substrings.

    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
    }