Jump to content

I'm doing this program for an assignment, where I have to read vowels from Strings and replace them with an int (counter). Counter must increment with every vowel replaced, until it reaches a value higher than 9 (at that point it is reset to 0).

 

All of the vowels are successfully being found, but counter is not working correctly and I can't figure out why. (Ex. strThree after method should output 0123456 but it outputs all zeros.)

 


public class VowelReplacement
{
  public static String vowelReplace(String input)
  {
    String vowels = "aeiou";
    int counter = 0;
   
      for (int x=0;x<input.length();x++)
      {
        for (int y=0;y<vowels.length();y++)
        {
          if (input.charAt(x) == vowels.charAt(y))
          {
            input = input.replace(input.substring(x, x+1), Integer.toString(counter));
            counter++;
          }
        } 
        if (counter > 9)
        {
          counter = 0;
        }
    }
    return input;
  }
  public static void main(String [] args)
  {
    String strOne = "abcdef";
    String strTwo = "hhhhhhh";
    String strThree = "aaaaaaa";
    String strFour = "catpigdatrathogbogfrogmoosegeese";
    String strFive = "hhhhhhh1234356HHHHDH";
    String strSix = "AEIOUaeiou87878alkjdaslwlejrlajflawjkflwj";
    String strSeven = "";
    String strEight = "x";
    String strNine = "0";
    
    System.out.println(vowelReplace(strOne));
    System.out.println(vowelReplace(strTwo));
    System.out.println(vowelReplace(strThree));
    System.out.println(vowelReplace(strFour));
    System.out.println(vowelReplace(strFive));
    System.out.println(vowelReplace(strSix));
    System.out.println(vowelReplace(strSeven));
    System.out.println(vowelReplace(strEight));
    System.out.println(vowelReplace(strNine));
  }
}

rubber dome apologist

Link to comment
https://linustechtips.com/topic/871327-vowelreplacement-class-java/
Share on other sites

Link to post
Share on other sites

You need to break out of the vowel (y) loop after you find a vowel.

 

You could also do something like this as it would be faster and more straightforward: 

public static String replaceVowels(String input){
  int counter = 0;
  String vowels = "aeiou";
  String output = "";
  for(int i=0; i<input.length(); i++){
      if(vowels.contains(input.charAt(i)){
          output += Integer.toString(counter);
          counter = (counter+1)%10;
      } else {
          output += input.charAt(i);                         
      }
  }
  return output;
}

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

12 minutes ago, djdwosk97 said:

You need to break out of the vowel (y) loop after you find a vowel.

Ok, even after fixing the stupid thing I did, and incorporating what you said, I still am getting same result. Is there something I'm missing?


public class VowelReplacement
{
  public static String vowelReplace(String input)
  {
    String vowels = "aeiou";
    int counter = 0;
    int y = 0;
    boolean breakOut = false;
   
      for (int x=0;x<input.length();x++)
      {
        while (y<vowels.length() && !breakOut)
        {
          if (input.charAt(x) == vowels.charAt(y))
          {
            input = input.replace(input.substring(x, x+1), Integer.toString(counter));
            counter++;
            breakOut = true;
          }
          y++;
        } 
        if (counter > 9)
        {
          counter = 0;
        }
        y = 0;
    }
    return input;
  }
  public static void main(String [] args)
  {
    String strOne = "abcdef";
    String strTwo = "hhhhhhh";
    String strThree = "aaaaaaa";
    String strFour = "catpigdatrathogbogfrogmoosegeese";
    String strFive = "hhhhhhh1234356HHHHDH";
    String strSix = "AEIOUaeiou87878alkjdaslwlejrlajflawjkflwj";
    String strSeven = "";
    String strEight = "x";
    String strNine = "0";
    
    System.out.println(vowelReplace(strOne));
    System.out.println(vowelReplace(strTwo));
    System.out.println(vowelReplace(strThree));
    System.out.println(vowelReplace(strFour));
    System.out.println(vowelReplace(strFive));
    System.out.println(vowelReplace(strSix));
    System.out.println(vowelReplace(strSeven));
    System.out.println(vowelReplace(strEight));
    System.out.println(vowelReplace(strNine));
  }
}

rubber dome apologist

Link to post
Share on other sites

3 minutes ago, WubGuy said:

Ok, even after fixing the stupid thing I did, and incorporating what you said, I still am getting same result. Is there something I'm missing?

 


public class VowelReplacement
{
  public static String vowelReplace(String input)
  {
    String vowels = "aeiou";
    int counter = 0;
    int y = 0;
    boolean breakOut = false;
   
      for (int x=0;x<input.length();x++)
      {
        while (y<vowels.length() && !breakOut)
        {
          if (input.charAt(x) == vowels.charAt(y))
          {
            input = input.replace(input.substring(x, x+1), Integer.toString(counter));
            counter++;
            breakOut = true;
          }
          y++;
        } 
        if (counter > 9)
        {
          counter = 0;
        }
        y = 0;
    }
    return input;
  }
  public static void main(String [] args)
  {
    String strOne = "abcdef";
    String strTwo = "hhhhhhh";
    String strThree = "aaaaaaa";
    String strFour = "catpigdatrathogbogfrogmoosegeese";
    String strFive = "hhhhhhh1234356HHHHDH";
    String strSix = "AEIOUaeiou87878alkjdaslwlejrlajflawjkflwj";
    String strSeven = "";
    String strEight = "x";
    String strNine = "0";
    
    System.out.println(vowelReplace(strOne));
    System.out.println(vowelReplace(strTwo));
    System.out.println(vowelReplace(strThree));
    System.out.println(vowelReplace(strFour));
    System.out.println(vowelReplace(strFive));
    System.out.println(vowelReplace(strSix));
    System.out.println(vowelReplace(strSeven));
    System.out.println(vowelReplace(strEight));
    System.out.println(vowelReplace(strNine));
  }
}

 

The issue is with input.replace(). It will replace any instance of the substring with the counter. The substring being 'aa', so it will replace all 'a's with 0 on the first iteration, and then it will end. 

 

Instead of using .replace, I'd build a new string like what I did in my first post.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×