Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vowel Counter Replaced in String Java

I was wondering how to fix my vowel counter program, I've searched through many forums but haven't had any luck. The lab's description is to "change all of the vowels in the String to numbers. Make sure the numbers range from 0-9 and that you reset the number to 0 when you get to a count > 9." So a sample input would be "abcdef" and the sample output would be "0bcd1f."

My Main Code is

public class VowelCounter
{
    public static String getNumberString(String s)
    {
       int counter = 0;
          for(int i = 0; i<s.length(); i++)
          {char g = s.charAt(i);
            if(g =='a') 
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='e')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='i')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='o')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='u')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='A')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='E')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='I')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='O')
            {
                counter++;
                s.replace(g,counter);
            }
            else if(g =='U')
            {
                counter++;
                s.replace(g,counter);
            }  
          }   
       return s;
    }
}

The Runner Class is

public class VowelCounterRunner
{
    public static void main ( String[] args )
    {
        System.out.println( VowelCounter.getNumberString("abcdef") );
        System.out.println( VowelCounter.getNumberString("hhhhhhh") );
        System.out.println( VowelCounter.getNumberString("aaaaaaa") );      
        System.out.println( VowelCounter.getNumberString("catpigdatrathogbogfrogmoosegeese") );
        System.out.println( VowelCounter.getNumberString("hhhhhhh1234356HHHHDH") );
        System.out.println( VowelCounter.getNumberString("AEIOUaeiou87878alkjdaslwlejrlajflawjkflwj") );
        System.out.println( VowelCounter.getNumberString("") );
        System.out.println( VowelCounter.getNumberString("x") );
        System.out.println( VowelCounter.getNumberString("e") );

    }
}

The error I am experiencing is with the replace method. It says that there is "no suitable method found for replace(char,int), method java.lang.String.replace(char,char) is not applicable; (argument mismatch; possible lossy conversion from int to char) This is my first programming class,so I'm honestly clueless on how to fix this.

like image 323
Leila Jones Avatar asked Jan 05 '16 02:01

Leila Jones


3 Answers

You are calling replace with an int value - java will not auto convert this int to a character. You need to explicitly convert the int to a human readable char. Here is a quick and dirty fix:

Change all the s.replace calls to

s = s.replace(g,(counter+"").charAt(0));

You will find there are still other problems in your code, even when the replace is fixed. Code that should work:

s = s.substring(0,i) + counter + s.substring(i);

The reason this does not require an explicit conversion of the int value is because the concatentation operator (+) converts it to text for you.

Summary:

  • In Java, types do not simply mix - int != char, you must tell the compiler how you want it to be converted

  • s.replace will not cause anything in s to change, you need to set s to the result of s.replace - kind of hard to wrap your head around when you're beginning, but you'll get it with practice

  • s.replace will replace all instances of a letter in that string with the same thing, which I don't think you want

like image 155
sunny-lan Avatar answered Nov 06 '22 12:11

sunny-lan


Java String is immutable, and you aren't assigning the result of replace (but I would prefer a StringBuilder, because it is mutable). Next, using a switch is, again in my opinion, easier to read here. As is a for-each loop. Putting that together, it might look something like

public static String getNumberString(String s) {
    if (s == null || s.isEmpty()) {
        return "";
    }
    StringBuilder sb = new StringBuilder(s.length());
    int counter = 0;
    for (char ch : s.toCharArray()) {
        switch (Character.toLowerCase(ch)) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
            sb.append(counter);
            counter++;
            if (counter > 9) {
                counter = 0;
            }
            break;
        default:
            sb.append(ch);
        }
    }
    return sb.toString();
}
like image 31
Elliott Frisch Avatar answered Nov 06 '22 12:11

Elliott Frisch


The compilation error is giving you a clue (Refer to javadocs on the replace method). You are trying to use integer for replacing a character, what you need to do is to replace the vowel with the character value of the number like '1' or '2' etc.

Also calling replace on string will return a new copy of the string so even though you are calling on replace you'll not get the desired effect. Think a bit different here (arrays, StringBuffers etc.)

like image 22
Aniruddh Dikhit Avatar answered Nov 06 '22 12:11

Aniruddh Dikhit