PROBLEM: Write a program to accept a sentence in mixed case. Find the frequency of vowels in each word and print the words along with their frequencies in separate lines.
MY CODE:
import java.util.Scanner;
public class wordm {
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println(“Enter a String”);
while(in.hasNext()){
int v=0;
String word=in.next();
int l=word.length();
if(word.equals(“.”))
break;
for(int i=0;i<=l;i++){
char ch=word.charAt(i);
if(ch==’a’||ch==’e’||ch==’i’||ch==’o’||ch==’u’||ch==’A’||ch==’E’||ch==’I’||ch==’O’||ch==’U’)
v++;
}
System.out.println(word+”t t ” +”v”);
}
}
}
OUTPUT:
Enter a String
This pisses me off.
Exception in thread “main” java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
at java.base/java.lang.String.charAt(String.java:1512)
at wordm.main(wordm.java:14)
This is really pissing me off! Any help will be welcomed