java - Counting Vowels, Repetition method -
java - Counting Vowels, Repetition method -
public static void main(string args[]) { scanner sc = new scanner(system.in); int countvowel=0; int countvowela=0; int countvowele=0; int countvoweli=0; int countvowelo=0; int countvowelu=0; char ch; string str; system.out.println("please come in string : "); str = sc.nextline(); for(int = 0; i<=str.length(); ++) { ch = str.charat(i); if(ch == 'a' || ch =='a') { countvowela++; countvowel++; } if(ch == 'e' || ch =='e') { countvowele++; countvowel++; } if(ch == 'i' || ch =='i') { countvoweli++; countvowel++; } if(ch == 'o' || ch =='o') { countvowelo++; countvowel++; } if(ch == 'u' || ch =='u') { countvowelu++; countvowel++; } i++; } system.out.println("occurances of in given string : " +countvowela); system.out.println("occurances of e in given string : " +countvowele); system.out.println("occurances of in given string : " +countvoweli); system.out.println("occurances of o in given string : " +countvowelo); system.out.println("occurances of u in given string : " +countvowelu); system.out.println("number of vowels in strings : " +countvowel); } }
for me having trouble, let's illustration if type lebron james best basketball player, u know it. gives me error , doesn't count vowels? also, can u tell if code right
check line below
for(int = 0; i<=str.length(); ++)
change to
for(int = 0; i<str.length(); ++)
why?
because in java
, index start zero. when have i <= str.length
, goes beyond scope index of string , gives java.lang.stringindexoutofboundsexception
another issue, have incremented variable i
twice. sec after if clauses totally unnecessary because gives wrong reply if rectify boundary issue.
java bluej
Comments
Post a Comment