I want a Java program that prompts the user to enter a sentence, and then replace all consonant (non vowel) letters in the sentence by the character ‘*’.
and I have been trying to solve it with nested for and if statement only and the final version of my code is
import java.util.*;
public class Q3{
public static void main (String [] args){
Scanner in = new Scanner (System.in);
String sentence;
char ch='x'; // I named the letters ch.
System.out.print("Enter a sentence: ");
sentence=in.nextLine();
int j = sentence.length();
for(int i =0;i<j;i++)
{
ch=sentence.charAt(i);
if (ch!='e' || ch!='a' || ch!='i' || ch!='u' || ch!='o' || ch!='E' || ch!='A' || ch!='I' || ch!='U' || ch!='O')
{
sentence.replace(ch,'*');
continue;
}
}
System.out.println(sentence.replace(ch,'*'));
}
}
but the problem is that when I type lama
the output is l*m*
and when I type The Java Programming language is fun
the output is The Java Programmi*g la*guage is fu*