Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!