Search code examples
javastringreplaceall

replace characters, one input multiple words


i have figured out a way to replace vowels into * but it only converts the first line

input: break robert yeah

output: br**k

here is the code

public class Solution {

    public static void main(String[] args) {
        String enterWord;
        Scanner scan = new Scanner (System.in);
        enterWord = scan.nextLine();
        enterWord = enterWord.replaceAll("[aeiou]", "*");
        
    System.out.println(enterWord);
    }
}

is there any way that it reads all three words?


Solution

  • You need a loop to keep getting and processing the inputs. Also, I suggest you use (?i) with the regex to make it case-insensitive.

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            String enterWord, answer = "y";
            Scanner scan = new Scanner(System.in);
            do {
                System.out.print("Enter a word: ");
                enterWord = scan.nextLine();
                enterWord = enterWord.replaceAll("(?i)[aeiou]", "*");
                System.out.println("After replacing vowels with * it becomes " + enterWord);
                System.out.print("Do you wish to conntinue[y/n]: ");
                answer = scan.nextLine();
            } while (answer.equalsIgnoreCase("y"));
        }
    }
    

    A sample run:

    Enter a word: hello
    After replacing vowels with * it becomes h*ll*
    Do you wish to conntinue[y/n]: y
    Enter a word: India
    After replacing vowels with * it becomes *nd**
    Do you wish to conntinue[y/n]: n
    

    For a single string spanning multiple lines, the method, String#replaceAll works for the entire string as shown below:

    public class Main {
        public static void main(String[] args) {
            String str = "break\n" + 
                        "robert\n" + 
                        "yeah";
            System.out.println(str.replaceAll("(?i)[aeiou]", "*"));
        }
    }
    

    Output:

    br**k
    r*b*rt
    y**h
    

    Using this feature, you can build a string of multiple lines interactively and finally change all the vowels to *.

    Demo:

    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            String text = "";
            Scanner scan = new Scanner(System.in);
            StringBuilder sb = new StringBuilder();
            System.out.println("Keep enter some text (Press Enter without any text to stop): ");
            while (true) {
                text = scan.nextLine();
                if (text.length() > 0) {
                    sb.append(text).append(System.lineSeparator());
                } else {
                    break;
                }
            }
    
            System.out.println("Your input: \n" + sb);
            String str = sb.toString().replaceAll("(?i)[aeiou]", "*");
            System.out.println("After converting each vowel to *, your input becomes: \n" + str);
        }
    }
    

    A sample run:

    Keep enter some text (Press Enter without any text to stop): 
    break
    robert
    yeah
    
    Your input: 
    break
    robert
    yeah
    
    After converting each vowel to *, your input becomes: 
    br**k
    r*b*rt
    y**h