Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Switch case increment only adding once to array

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');
        }                  
    }
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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
over 4 years ago · Santiago Trujillo Relatório

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda