Anyway I'm trying to add an extension to my program in my class that will count how many words there are in a given string. I've done about an hour of research and I found out about the split command. I don't know how to fully use it but I think I got it down correctly in my code. But I am having another problem.
I am getting an outofbounds error on the line where the if statement starts:
AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5
The 5 at the end is uncoincidentally the number of words that I enter in the sentence
I tried to get it so that when I push a button it will tell me the # of words. Oh and should I be displaying "count" as the # of words?
if(e.getSource()==b4)
{
s6=t7.getText();
sv=s6.length();
String []splitter=s6.split(" ");
int count=0;
for(int i=0;i<sv;i++)
{
if(splitter[i].equals(" "))
{}
else
{count++;}
}
}
I tried but we just started string manipulation and haven't actually learned the split command. Thanks for any assistance!

New Topic/Question
Reply
MultiQuote










|