0

I have to get a text input from the user and then I have to replace every vowel a,e,i,o,u for 1,2,3,4,5 and show the amount of replacements for every vowel and then also the changed text. The problem is the text at the end looks nicely replaced but the amount of replacements show up as 1.

(I tried doing it below counting the 1,2,3,4 and 5s in "myText" and it works perfect but it also counts if the user inputs a number so that is a problem)

Heres the first part:

public static void replaceAndCount(String myText, int[] vowels) {

    for (int i = 0; i < myText.length(); i++) {            
        switch (myText.charAt(i)) {
            case 'a':
                vowels[0]++;
                myText = myText.replace('a', '1');              
            case 'e':
                vowels[1]++;
                myText = myText.replace('e', '2');                    
            case 'i':
                vowels[2]++;
                myText = myText.replace('i', '3');                    
            case 'o':
                vowels[3]++;
                myText = myText.replace('o', '4');                    
            case 'u':
                vowels[4]++;
                myText = myText.replace('u', '5');
        }                  
    }
}
Leonardo
  • 2,439
  • 33
  • 17
  • 31
Tensai
  • 1
  • I fixed it using `char[] myTextChar = myText.toCharArray()` after the for and before the switch, then used `myTextChar[i] = '1';` inside the case and right after the switch I used `myText = String.valueOf(myTextChar);` – Tensai Nov 14 '21 at 17:09

2 Answers2

1

You have 2 issues in your code.

  1. You have to use break in each case, otherwise all the cases below the matching case will be executed as well. Example,
case 'a':
    vowels[0]++;
    myText = myText.replace('a', '1'); 
    break;          
case 'e':
    vowels[1]++;
    myText = myText.replace('e', '2');
    break;
  1. String replace(old,new) will replace all occurrences of the old character with new. That is why, amount of replacements show up as 1.
    Refer this for string replacement at specific index
sittsering
  • 1,219
  • 2
  • 6
  • 13
0

Hope this below code works for you

Code :

public class ReplaceVowels {

public static void main(String[] args) {
    String myText = "aaaeiou";
    int[] vowels = new int[5];
    replaceAndCount(myText.toLowerCase(), vowels);
}

private static void replaceAndCount(String text, int[] vowels) {
    StringBuilder myText = new StringBuilder(text);
    for (int i = 0; i < myText.length(); i++) {
        switch (myText.charAt(i)) {
            case 'a':
                vowels[0]++;
                myText.setCharAt(i, '1');
                break;
            case 'e':
                vowels[1]++;
                myText.setCharAt(i, '2');
                break;
            case 'i':
                vowels[2]++;
                myText.setCharAt(i, '3');
                break;
            case 'o':
                vowels[3]++;
                myText.setCharAt(i, '4');
                break;
            case 'u':
                vowels[4]++;
                myText.setCharAt(i, '5');
        }
    }
    for (int vowel : vowels) {
        System.out.println(vowel);
    }
    System.out.println(myText);
}

Results enter image description here

You can refer this for replacing single character

Sri
  • 437
  • 1
  • 4
  • 13