Leila Jones
Leila Jones

Reputation: 79

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.

Upvotes: 5

Views: 3206

Answers (4)

Let&#39;sRefactor
Let&#39;sRefactor

Reputation: 3346

Here you go,

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static final int ASCII_0 = 48;
    public static String getNumberString(String s)
    {
        int counter = 0;
        for(int i = 0; i<s.length(); i++)
        {
            char g = Character.toLowerCase(s.charAt(i));
            if(g =='a' || g =='e' || g =='i' || g =='o' || g =='u') 
            {
                StringBuilder sb = new StringBuilder(s);
                sb.setCharAt(i, (char) (counter + ASCII_0));
                s = sb.toString();
                counter = (counter + 1) % 10;
            }
        }   
      return s;
    }

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

will give an out-put of

0bcd1f
hhhhhhh
0123456
c0tp1gd2tr3th4gb5gfr6gm78s9g01s2
hhhhhhh1234356HHHHDH
0123456789878780lkjd1slwl2jrl3jfl4wjkflwj

x
0

Please have a look at my experiment as well : http://rextester.com/CEIUP1959

Upvotes: 0

user5182794
user5182794

Reputation:

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

Upvotes: 5

Aniruddh Dikhit
Aniruddh Dikhit

Reputation: 682

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.)

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201467

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();
}

Upvotes: 4

Related Questions