As a java beginner, I'm trying to complete a simple exercise to remove all instances of the character 'a' from every element in an array of strings, and then print all elements.
The below code, simply outputs the array unchanged (still containing a's).
I believe I've found an alternative method of achieving it, but I still can't see why the below fails, and for learning purposes would like to know. Any advice would be appreciated.
public static void main(String[] args) {
String instruments[] = {"cello", "guitar", "violin", "double bass"};
for (int i = 0; i < instruments.length; i++) {
String str = instruments[i];
for (int b = 0; b < str.length(); b++) {
char a = str.charAt(b);
if (str.charAt(b) == 'a') {
str.replace("a", "");
}
}
System.out.println(str);
}
}