java - code not exiting outer for loop -
java - code not exiting outer for loop -
my code isn't exiting outer loop when come end of string , cannot figure out why. understanding, outer loop should stop after 4th iteration. instead, continues , errors out @ string inputstring = input.next(); because there nil there. here code:
public class exercise17 { public static void main(string[] args) { string string = "i think, hence am"; vowelcount(string); } public static void vowelcount(string s) { scanner input = new scanner(s); int[] vowelarray = new int[5]; int acount = 0, ecount = 0, icount = 0, ocount = 0, ucount = 0; for(int = 0; < s.trim().length() - 1; i++) { string inputstring = input.next(); system.out.println(inputstring); for(int j = 0; j < inputstring.length(); j++) { char c = inputstring.charat(j); system.out.println(c); if(c == 'a') { acount++; }else if (c == 'e') { ecount++; }else if (c == 'i') { icount++; }else if (c == 'o') { ocount++; }else if (c == 'u') { ucount++; } } } vowelarray[0] = acount; vowelarray[1] = ecount; vowelarray[2] = icount; vowelarray[3] = ocount; vowelarray[4] = ucount; input.close(); system.out.println(arrays.tostring(vowelarray)); } }
as experiment have tried rid of -1 , create < s.trim().length() changing < s.trim().length() -2, < s.trim().length() -3, < s.trim().length() -4, etc., way -10.
scanner.next()
reads content of input string word word (i.e. "i", "think", "therefore", "i", "am") counting outer loop letter letter (with respect input string). next()
phone call throws exception when input string exhausted, after 5th iteration.
java for-loop
Comments
Post a Comment