Vowel Counter Replaced by String Java

I was wondering how to fix my program for checking vowels, I searched many forums, but I had no luck. The lab description should "change all the vowels in the string to numbers. Make sure the numbers range from 0 to 9 and that you reset the number to 0 when you hit the number> 9." Thus, the sample input will be "abcdef" and the sample output will be "0bcd1f."

My main code

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

Class runner

 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 encountered is a replacement method. It says that "there is no suitable method for replacing (char, int), the java.lang.String.replace (char, char) method is not applicable; (argument mismatch, possible lossy conversion from int to char) This is my first programming class so I honestly don't know how to fix this.

+5
source share
4 answers

You cause a replacement with an int value - java will not automatically convert this int to a character. You need to explicitly convert int to human readable char. Here is a quick and dirty fix:

Change all s.replace calls to

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

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

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

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

Summary:

  • In Java, types don't just mix - int != char , you must tell the compiler how you want to convert it

  • s.replace won't change anything in s , you need to set s to the result of s.replace - somehow it's hard to wrap your head around when you start, but you will get it with practice

  • s.replace will replace all letter instances in this line with the same one that I don’t think you want

+5
source

Java String is immutable, and you do not assign a result to replace (but I would prefer StringBuilder as it is modified). Further, using switch , again, in my opinion, it is easier to read here. As there is a for-each loop. Combining this, it may 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(); } 
+5
source

A compilation error gives you a key (see javadocs in the replace method). You are trying to use an integer to replace a character, what you need to do is replace the vowel with the character value of the number, for example "1" or "2", etc.

Also, the call to replace in a string returns a new copy of the string, so even if you call a replacement, you will not get the desired effect. Think a bit differently (arrays, StringBuffers, etc.)

+1
source

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 a way

 0bcd1f hhhhhhh 0123456 c0tp1gd2tr3th4gb5gfr6gm78s9g01s2 hhhhhhh1234356HHHHDH 0123456789878780lkjd1slwl2jrl3jfl4wjkflwj x 0 

Please also look at my experiment: http://rextester.com/CEIUP1959

0
source

Source: https://habr.com/ru/post/1239847/


All Articles