我想找到一个句子中每个单词的字母数。我已经尝试了几次使用几个代码,但从来没有得到它。我总是显示错误StringIndexOutOfBounds异常。我的一个代码如下:java程序找到一个句子中每个单词的字母数
import java.util.*;
class LengthOfEachWord
{
public static void main()
{
Scanner sc = new Scanner(System.in);
String s,a,b;
int l,x,l1,y=0,now;
try
{
System.out.print("Enter a sentence:");
s=sc.nextLine();
l=s.length();
String arr[]=s.split(" ");
now=arr.length;
while(true)
{
x=s.indexOf(' ');
a=s.substring(0,x);
l1=a.length();
System.out.println(a+"="+l1);
b=s.substring(x+1,l);
s=b;
y++;
if(now==y)
{
break;
}
}//End of while block
}//End of try block
catch(Exception e)
{
System.out.println("Error="+e);
}
}
}