Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda