So Im having an issue where my code will not remove special characters except spaces. I have tried subtracing indexes and even adding index and it still will not work. I have tried an input "BANANA & PEEL" and it wont work.
String strMessage = input.nextLine();
String strChars = " !@#$%^&&*() ";
for(int i = 0; i < strMessage.length() - 1; i++)
for(int j = 0; j < strChars.length(); j++) {
if(strChars.charAt(j) == strMessage.charAt(i)) {
strMessage = strMessage.substring(0, strMessage.indexOf(strMessage.charAt(i))) + strMessage.substring(strMessage.indexOf(strMessage.charAt(i+1)));
break;
}
}
System.out.println(strMessage);
So my output should be BANANAPEEL