I have a for loop to remove the vowels from a string, but I get an error if the string ends in a vowel. It works if the string doesn't end in a vowel and prints out the results just fine, but if it ever ends with a vowel it will not work and I get the error. How could I fix this?
package program5;
import java.util.Scanner;
public class Disemvoweling {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String phrase;
System.out.println("Welcome to the disemvoweling utility.");
System.out.print("Enter your phrase: ");
phrase = scnr.nextLine();
int inputLength = phrase.length();
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == 'a') {
phrase = phrase.replace("a","");
}
if (phrase.charAt(i) == 'e') {
phrase = phrase.replace("e","");
}
if (phrase.charAt(i) == 'i') {
phrase = phrase.replace("i","");
}
if (phrase.charAt(i) == 'o') {
phrase = phrase.replace("o","");
}
if (phrase.charAt(i) == 'u') {
phrase = phrase.replace("u","");
}
}
System.out.println("The disemvolwed phrase is: " + phrase);
int inputAfter = phrase.length();
System.out.print("Reduced from " + inputLength + " characters to " + inputAfter + " characters. ");
double percentage = (double) inputAfter / inputLength * 100;
double percentageRounded = (double) percentage % 1;
System.out.print("Reduction rate of " + (percentage - percentageRounded) + "%");
}
}