-1

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*

Thilo
  • 257,207
  • 101
  • 511
  • 656
Lama
  • 1
  • 1
  • 2
    Read the javadoc of String.replace very carefully. Does it modify the string? Also, `ch!='e' || ch!='a'` is always true: If it's a, then `ch!='e'` is true. If it's e, then `ch!='a'` is true. – JB Nizet Oct 26 '19 at 09:08
  • 1
    I must add that your strategy is very inefficient. Why not use a StringBuilder to append every char, or its replacement, of the original string? Your code makes multiple passes on the string and creates a lot of copies. – JB Nizet Oct 26 '19 at 09:09
  • [How do I check if a char is a vowel?](https://stackoverflow.com/questions/19160921/how-do-i-check-if-a-char-is-a-vowel) – Abra Oct 26 '19 at 09:15
  • [What's the best way to check if a character is a vowel in Java?](https://stackoverflow.com/questions/26557604/whats-the-best-way-to-check-if-a-character-is-a-vowel-in-java/51334653) – Abra Oct 26 '19 at 09:19
  • 1
    `sentence.replace(ch,'*');` does not do anything useful if you don't assign the result somewhere. – Thilo Oct 26 '19 at 09:32
  • Why do you do that final `replace` outside of the loop? And what do you think will be the value of `ch` there? – Thilo Oct 26 '19 at 09:34
  • Strings are immutable. There is no method that can modify them. Instead, `replace` returns the resulting string. Again, it does **not** modify it. – Zabuzard Oct 26 '19 at 09:50
  • This looks like a typical homework assignment to me. 'What is wrong with this code'. The class name Q3 indicates 'Question 3'. There are several (obvious) errors in the code and the suggested strings ('The Java Programming language is fun') also clearly indicate some form of an assignment. If that is the case, just be clear about that. In any case, there are several duplicates of this question. – Thizzer Oct 26 '19 at 09:52

1 Answers1

1

First of all,you should replace || with && in your if statement,because,as JB Nizet mentioned ,the if you have now,always returns true.Second,the replace function,replaces all the occurrences of ch char and you must assign the result of it somewhere.

sentence = sentence.replace(ch,'*');