import java.util.Scanner; public class program4f_4 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a string to see how many times each vowel occurs."); String message = sc.nextLine(); message = message.toLowerCase(); int length = message.length(); vowelFrequency(message, length); } static public void vowelFrequency(String message, int length) { int vowelA=0; int vowelE=0; int vowelI=0; int vowelO=0; int vowelU=0; int x = 0; char vowel; for(x = 0; x <= length; x++) { vowel = message.charAt(x); if (vowel == 'a') vowelA++; if (vowel == 'e') vowelE++; if (vowel == 'i') vowelI++; if (vowel == 'o') vowelO++; if (vowel == 'u') vowelU++; } System.out.println("Vowel Occurances"); System.out.println("A: " + vowelA); System.out.println("E: " + vowelE); System.out.println("I: " + vowelI); System.out.println("O: " + vowelO); System.out.println("U: " + vowelU); } }
When I run it it highlights "vowel = message.charAt(x);" and the error message is
"java.lang.StringIndexOutofBoundsException:
String index out of range: 10 (in java.lang.String)". I'm thinking it has something to do with char variable type?