Bastard24 Bastard24 - 1 month ago 17
Java Question

StringBuilder insert method

I'm attempting to insert a character to the beginning of each substring Instead of inserting at position 0 of the substring, the method is inserting the character(s) to the beginning of the entire string. For example:

Input: esttar apple%hc orange%hc annanabar eachpar
Expected output: test apple orange banana peach
Actual output: pbtest apple orange anana each


Everything works as intended besides the StringBuilder insert method. My code is below. Many thanks ahead of time.

private String decryptText(String encrypted, String cipher){
StringBuilder stringBuilder = new StringBuilder(); //for manipulating the substrings
StringBuilder builder2 = new StringBuilder(); //StringBuilder object for returning and accumulating instances of decrypted
String decryptedSS = "";

cipher = cipher.replaceAll("-", ""); //replaces all the hyphens
char[] cipherKey = cipher.toCharArray(); //converts the cipherKey to a character array
char y1 = cipherKey[2];


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

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

stringBuilder.append(subStr); //copies the substring into a String Builder object

stringBuilder = stringBuilder.deleteCharAt(stringBuilder.length()-1); //deletes the last character
stringBuilder = stringBuilder.deleteCharAt(stringBuilder.length()-1); //deletes the last character

char first = stringBuilder.charAt(stringBuilder.length()-1); //copies the last character for prepending to the word if the word started with a consonant


if(stringBuilder.charAt(stringBuilder.length()-1) == y1){ //if the last character is equal to y1
stringBuilder.deleteCharAt(stringBuilder.length()-1); //delete it

}
else{ //******The problem resides in this else statement
stringBuilder.deleteCharAt(stringBuilder.length()-1); //delete the last character
stringBuilder.insert(0, first); //insert the copied character at the beginning of the substring
}

stringBuilder.append(" "); //appends a space to each word
decryptedSS = stringBuilder.toString(); //converts the StringBuilder object to a string
}

builder2.append(decryptedSS); //appends the decrypted substring to the StringBuilder object to concatenate the string

String decrypted = builder2.toString(); //converts the StringBuilder object to a string

return decrypted; //returns the decrypted string
}

Answer

This line is the problem:

stringBuilder.insert(0, first); //insert the copied character at the beginning of the substring

the stringBuilder here does not represent the current substring, but instead the final decrypted string that you are trying to build. That is why the value is getting added to the beginning of the entire string.

You have several options:

  1. You can add the resulting string from stringBuilder at the end of the loop to builder2, then clear the stringBuilder. From how your code was written, I imagine this is what you were expecting:

    builder2.append(decryptedSS); 
    stringBuilderObj.setLength(0); 
    

    Note that you would also remove the builder2.append(decryptedSS); that you have outside the loop.

  2. You could save the length of stringBuilder at the start of the loop, and insert into that value instead of 0.

  3. You can fully modify subStr in your loop, and append the completed version to stringBuilder at the end of the loop.

Comments