Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Did you mean:</p> <pre><code>public int countVowels(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { switch(c) { case 'a': vowels++; break; case 'e': vowels++; break; case 'o': vowels++; break; case 'u': vowels++; break; case 'i': vowels++; break; case 'y': vowels++; break; default: } } return vowels; } </code></pre> <p>??? I cannot speak very good in english, I used Translate Google</p> <p>EDIT: Second method and shorter:</p> <pre><code>public static int countVowels(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { if("euioa".contains(c+""))vowels++; } return vowels; } </code></pre> <p>Second edit: if your String has more chars than about 10000 and using recursive, you have a exception, for example:</p> <pre><code>Exception in thread "main" java.lang.StackOverflowError at sun.nio.cs.SingleByte.withResult(Unknown Source) at sun.nio.cs.SingleByte.access$000(Unknown Source) at sun.nio.cs.SingleByte$Encoder.encodeArrayLoop(Unknown Source) at sun.nio.cs.SingleByte$Encoder.encodeLoop(Unknown Source) at java.nio.charset.CharsetEncoder.encode(Unknown Source) at sun.nio.cs.StreamEncoder.implWrite(Unknown Source) at sun.nio.cs.StreamEncoder.write(Unknown Source) at java.io.OutputStreamWriter.write(Unknown Source) at java.io.BufferedWriter.flushBuffer(Unknown Source) at java.io.PrintStream.write(Unknown Source) at java.io.PrintStream.print(Unknown Source) at java.io.PrintStream.println(Unknown Source) at Test2.main(Test2.java:24) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) at Test2.main(Test2.java:25) ... very very long </code></pre> <p>NEXT EDIT: I did test of speed count chars:</p> <pre><code>[Test #1] Result with recursive: 4.263765 ms [Test #1] Result without (using contains, faster method): 2.69513 ms [Test #1] Result without (using switch): 0.346898 ms [Test #1] Result without (using if): 0.423256 ms [Test #1] Result without (using indexOf): 0.644943 ms [Test #2] Result with recursive: 6.40468 ms [Test #2] Result without (using contains, faster method): 4.177144 ms [Test #2] Result without (using switch): 0.263149 ms [Test #2] Result without (using if): 0.281624 ms [Test #2] Result without (using indexOf): 0.453225 ms [Test #3] Result with recursive: 4.314261 ms [Test #3] Result without (using contains, faster method): 2.073998 ms [Test #3] Result without (using switch): 0.428183 ms [Test #3] Result without (using if): 0.36373 ms [Test #3] Result without (using indexOf): 0.392467 ms [Test #4] Result with recursive: 5.740032 ms [Test #4] Result without (using contains, faster method): 2.053882 ms [Test #4] Result without (using switch): 0.160107 ms [Test #4] Result without (using if): 0.171192 ms [Test #4] Result without (using indexOf): 0.230718 ms [Test #5] Result with recursive: 3.19105 ms [Test #5] Result without (using contains, faster method): 1.833838 ms [Test #5] Result without (using switch): 0.144096 ms [Test #5] Result without (using if): 0.16257 ms [Test #5] Result without (using indexOf): 0.210192 ms [Test #6] Result with recursive: 2.586339 ms [Test #6] Result without (using contains, faster method): 1.41715 ms [Test #6] Result without (using switch): 0.152718 ms [Test #6] Result without (using if): 0.161338 ms [Test #6] Result without (using indexOf): 0.220865 ms [Test #7] Result with recursive: 2.445117 ms [Test #7] Result without (using contains, faster method): 1.134295 ms [Test #7] Result without (using switch): 0.164212 ms [Test #7] Result without (using if): 0.083749 ms [Test #7] Result without (using indexOf): 0.133833 ms [Test #8] Result with recursive: 1.995997 ms [Test #8] Result without (using contains, faster method): 0.987325 ms [Test #8] Result without (using switch): 0.058295 ms [Test #8] Result without (using if): 0.084569 ms [Test #8] Result without (using indexOf): 0.130959 ms [Test #9] Result with recursive: 4.914866 ms [Test #9] Result without (using contains, faster method): 0.335403 ms [Test #9] Result without (using switch): 0.057063 ms [Test #9] Result without (using if): 0.085801 ms [Test #9] Result without (using indexOf): 0.142865 ms [Test #10] Result with recursive: 1.164673 ms [Test #10] Result without (using contains, faster method): 0.330477 ms [Test #10] Result without (using switch): 0.058295 ms [Test #10] Result without (using if): 0.180223 ms [Test #10] Result without (using indexOf): 0.129728 ms [Test #11] Result with recursive: 1.089547 ms [Test #11] Result without (using contains, faster method): 0.391646 ms [Test #11] Result without (using switch): 0.073074 ms [Test #11] Result without (using if): 0.307487 ms [Test #11] Result without (using indexOf): 0.123159 ms [Test #12] Result with recursive: 3.442706 ms [Test #12] Result without (using contains, faster method): 0.24755 ms [Test #12] Result without (using switch): 0.052548 ms [Test #12] Result without (using if): 0.204855 ms [Test #12] Result without (using indexOf): 0.123159 ms [Test #13] Result with recursive: 0.521373 ms [Test #13] Result without (using contains, faster method): 0.251655 ms [Test #13] Result without (using switch): 0.047211 ms [Test #13] Result without (using if): 0.073074 ms [Test #13] Result without (using indexOf): 0.115359 ms [Test #14] Result with recursive: 0.540258 ms [Test #14] Result without (using contains, faster method): 0.261508 ms [Test #14] Result without (using switch): 0.053779 ms [Test #14] Result without (using if): 0.0858 ms [Test #14] Result without (using indexOf): 0.083748 ms [Test #15] Result with recursive: 0.554626 ms [Test #15] Result without (using contains, faster method): 0.26315 ms [Test #15] Result without (using switch): 0.056653 ms [Test #15] Result without (using if): 0.078411 ms [Test #15] Result without (using indexOf): 0.079232 ms [Test #16] Result with recursive: 0.529994 ms [Test #16] Result without (using contains, faster method): 0.253298 ms [Test #16] Result without (using switch): 0.058706 ms [Test #16] Result without (using if): 0.086622 ms [Test #16] Result without (using indexOf): 0.087443 ms [Test #17] Result with recursive: 0.520552 ms [Test #17] Result without (using contains, faster method): 0.267255 ms [Test #17] Result without (using switch): 0.055832 ms [Test #17] Result without (using if): 0.086622 ms [Test #17] Result without (using indexOf): 0.084569 ms [Test #18] Result with recursive: 0.531636 ms [Test #18] Result without (using contains, faster method): 0.260687 ms [Test #18] Result without (using switch): 0.058706 ms [Test #18] Result without (using if): 0.082927 ms [Test #18] Result without (using indexOf): 0.088675 ms [Test #19] Result with recursive: 0.654385 ms [Test #19] Result without (using contains, faster method): 0.25494 ms [Test #19] Result without (using switch): 0.059117 ms [Test #19] Result without (using if): 0.090317 ms [Test #19] Result without (using indexOf): 0.084159 ms [Test #20] Result with recursive: 0.551342 ms [Test #20] Result without (using contains, faster method): 0.28655 ms [Test #20] Result without (using switch): 0.083748 ms [Test #20] Result without (using if): 0.111664 ms [Test #20] Result without (using indexOf): 0.081696 ms [Test #21] Result with recursive: 1.042336 ms [Test #21] Result without (using contains, faster method): 1.165084 ms [Test #21] Result without (using switch): 0.068969 ms [Test #21] Result without (using if): 0.095653 ms [Test #21] Result without (using indexOf): 0.089496 ms [Test #22] Result with recursive: 0.555447 ms [Test #22] Result without (using contains, faster method): 0.27054 ms [Test #22] Result without (using switch): 0.066095 ms [Test #22] Result without (using if): 0.091137 ms [Test #22] Result without (using indexOf): 0.329656 ms [Test #23] Result with recursive: 2.345769 ms [Test #23] Result without (using contains, faster method): 0.109611 ms [Test #23] Result without (using switch): 0.082106 ms [Test #23] Result without (using if): 0.09278 ms [Test #23] Result without (using indexOf): 0.094833 ms [Test #24] Result with recursive: 0.565711 ms [Test #24] Result without (using contains, faster method): 0.079643 ms [Test #24] Result without (using switch): 0.089906 ms [Test #24] Result without (using if): 0.082517 ms [Test #24] Result without (using indexOf): 0.088264 ms [Test #25] Result with recursive: 0.552573 ms [Test #25] Result without (using contains, faster method): 0.078001 ms [Test #25] Result without (using switch): 0.052137 ms [Test #25] Result without (using if): 0.095654 ms [Test #25] Result without (using indexOf): 0.089085 ms </code></pre> <p>Code of test:</p> <pre><code>static int i = 0; private static long start; private static long end; public static void main(String[] args) { for(int j=0; j&lt;25; j++) { test(); } } public static void test() { i++; String string = "aurwgn iowthpbomaj yo4jpb 4y9 b0q thaioe vau hewoimjnyoj ioajr " + "itwejb iwojaoehmtuwehtoawegtoam yierjspyomrahvuir auyheroiamvyirhapivthe " + "awibthnapeutmbhuewath huaheovgayeutn jeryhaipmvhtuwea htpaw thwuaurwgn iowthpbomaj yo4jpb 4y9 b0q thaioe vau hewoimjnyoj ioajr " + "itwejb iwojaoehmtuwehtoawegtoam yierjspyomrahvuir auyheroiamvyirhapivthe " + "awibthnapeutmbhuewath huaheovgayeutn jeryhaipmvhtuwea htpaw thwuaurwgn iowthpbomaj yo4jpb 4y9 b0q thaioe vau hewoimjnyoj ioajr " + "itwejb iwojaoehmtuwehtoawegtoam yierjspyomrahvuir auyheroiamvyirhapivthe " + "awibthnapeutmbhuewath huaheovgayeutn jeryhaipmvhtuwea htpaw thwuaurwgn iowthpbomaj yo4jpb 4y9 b0q thaioe vau hewoimjnyoj ioajr " + "itwejb iwojaoehmtuwehtoawegtoam yierjspyomrahvuir auyheroiamvyirhapivthe " + "awibthnapeutmbhuewath huaheovgayeutn jeryhaipmvhtuwea htpaw thwuaurwgn iowthpbomaj yo4jpb 4y9 b0q thaioe vau hewoimjnyoj ioajr " + "itwejb iwojaoehmtuwehtoawegtoam yierjspyomrahvuir auyheroiamvyirhapivthe " + "awibthnapeutmbhuewath huaheovgayeutn jeryhaipmvhtuwea htpaw thwu"; start = System.nanoTime(); countVowels2(string); end = System.nanoTime(); System.out.println("[Test #"+i+"] Result with recursive: "+(end-start)/1000000.0+" ms"); start = System.nanoTime(); countVowels(string); end = System.nanoTime(); System.out.println("[Test #"+i+"] Result without (using contains, faster method): "+(end-start)/1000000.0+" ms"); start = System.nanoTime(); countVowels3(string); end = System.nanoTime(); System.out.println("[Test #"+i+"] Result without (using switch): "+(end-start)/1000000.0+" ms"); start = System.nanoTime(); countVowels4(string); end = System.nanoTime(); System.out.println("[Test #"+i+"] Result without (using if): "+(end-start)/1000000.0+" ms"); start = System.nanoTime(); countVowels5(string); end = System.nanoTime(); System.out.println("[Test #"+i+"] Result without (using indexOf): "+(end-start)/1000000.0+" ms"); System.out.println(); } public static int countVowels(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { if("euioa".contains(c+""))vowels++; } return vowels; } public static int countVowels2(String s) { if(s.length()==0)return 0; if("euioa".contains(s.charAt(0)+""))return 1+countVowels2(s.substring(1)); return countVowels2(s.substring(1)); } public static int countVowels3(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { switch(c) { case 'a': vowels++; break; case 'e': vowels++; break; case 'o': vowels++; break; case 'u': vowels++; break; case 'i': vowels++; break; case 'y': vowels++; break; default: } } return vowels; } public static int countVowels4(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { if(c=='a'||c=='e'||c=='o'||c=='u'||c=='i'||c=='y')vowels++; } return vowels; } public static int countVowels5(String s) { int vowels = 0; s=s.toLowerCase(); for(char c : s.toCharArray()) { if("euioa".indexOf(c)!=-1)vowels++; } return vowels; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload