You have problems with your indexes. My suggestions:
- The for loop needs to iterate over the length of the second string in
charArray
(charArray holds the string not the characters!). - You can use a Stringbuilder to remove the character at position i instead of doing your two
getWord.substring()
.
Here is what I suggest for the code in the for loop:
String[] charArray = currentItem.split("\\+");
for(int i = 0; i < charArray[1].length(); i++){
String getWord = charArray[charArray.length - 1];
StringBuilder sb = new StringBuilder(getWord);
String addWord = (getWord.charAt(i) + charArray[0] + "+" + sb.deleteCharAt(i).toString());
System.out.println(addWord);
permStack.push(addWord);
}
Try this and let me know if this is what you want.