/    Sign up×
Community /Pin to ProfileBookmark

“java.lang.StringIndexOutOfBoundsException” error in java

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

to post a comment
Java

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 01.2022 — Since you're starting with index 0 in your for loop, you probably want i &lt; l instead of i &lt;= l in the for loop initialization.
×

Success!

Help @keyboard_music spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...