与えられた文字列で、私は最も長い単語を見つけて同じものを印刷したいと思います。Java:与えられた文字列の中で最も長い単語を見つけるには?
出力が2番目に長い単語、つまり"Today"
ですが、私は"Happiest"
になるはずです。
私が間違っていることを知っているかもしれませんか、または文字列の中で最も長い単語を見つけるためのより良い/異なる方法がありますか?
public class DemoString
{
public static void main(String[] args)
{
String s="Today is the happiest day of my life";
String[] word=s.split(" ");
String rts=" ";
for(int i=0;i<word.length;i++){
for(int j=1+i;j<word.length;j++){
if(word[i].length()>=word[j].length()){
rts=word[i];
}
}
}
System.out.println(rts);
System.out.println(rts.length());
}
}