1

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?

1
  • Is the input single line? are break robert yeah on same input line? If yes, then your code is giving br**k r*b*rt y**h in my environment. If no, then put 3 lines from scan.nextLine(); inside a loop. Commented Mar 25, 2021 at 4:21

2 Answers 2

1

Your code works as you want (in:break robert yeah out: br**k r*b*rt y**h) on my env(Windows10, java1.8.0_271), maybe you can set a breakpoint on enterWord = enterWord.replaceAll("[aeiou]", "*"); and check is the enterWord recived whole input string.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.