Java:如何找到给定字符串中最长的单词?

时间:2017-04-06 09:59:20

标签: java

在给定的字符串中,我想找到最长的单词并打印相同的单词。

我得到的输出是第二个最长的单词,即"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 longword=" ";
        for(int i=0;i<word.length;i++){
            for(int j=1+i;j<word.length;j++){
                if(word[i].length()>=word[j].length()){
                    longword=word[i];
                }
            }
        }
        System.out.println(longword+" is longest word with "+longword.length()+" characters");
        System.out.println(rts.length());
    }
}

8 个答案:

答案 0 :(得分:3)

应该是:

for(int i=0;i<word.length;i++){
   if(word[i].length()>=rts.length()){
      rts=word[i];
   }
}

答案 1 :(得分:2)

这是一个可以与Java 8一起使用的单行程序:

import java.util.*;

class Main {
  public static void main(String[] args) {
    String s = "Today is the happiest day of my life";
    System.out.println(Arrays.stream(s.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null));
  }
}

<强>输出:

happiest

试试here!

答案 2 :(得分:2)

    String s= "Today is the happiest day of my life by vijayakumar";
           String [] word = s.split(" ");
    String maxlethWord = "";
    for(int i = 0; i < word.length; i++){
            if(word[i].length() >= maxlethWord.length()){
                  maxlethWord = word[i];
            } 
    }
     System.out.println(maxlethWord);  

答案 3 :(得分:2)

在创建单词列表时,我还没有看到答案。 因此,这是解决问题的另一种方法:

String s = "Today is the happiest day of my life";;
List<String> strings = Arrays.asList(s.split(" "));    
String biggestWord = Collections.max(strings, Comparator.comparing(String::length));
System.out.println(biggestWord);

输出:

happiest

答案 4 :(得分:1)

//下面的Java程序将在字符串中找到最小和最大的单词

class SmallestAndLargestWord 
{ 

    static String minWord = "", maxWord = ""; 

    static void minMaxLengthWords(String input)  
    { 
        // minWord and maxWord are received by reference  
        // and not by value 
        // will be used to store and return output 
        int len = input.length(); 
        int si = 0, ei = 0; 
        int min_length = len, min_start_index = 0, 
              max_length = 0, max_start_index = 0; 

        // Loop while input string is not empty 
        while (ei <= len)  
        { 
            if (ei < len && input.charAt(ei) != ' ') 
            { 
                ei++; 
            }  
            else
            { 
                // end of a word 
                // find curr word length 
                int curr_length = ei - si; 

                if (curr_length < min_length)  
                { 
                    min_length = curr_length; 
                    min_start_index = si; 
                } 

                if (curr_length > max_length)  
                { 
                    max_length = curr_length; 
                    max_start_index = si; 
                } 
                ei++; 
                si = ei; 
            } 
        } 

        // store minimum and maximum length words 
        minWord = input.substring(min_start_index, min_start_index + min_length); 
        maxWord = input.substring(max_start_index, max_length); 
    } 

    // Driver code 
    public static void main(String[] args) 
    { 
        String a = "GeeksforGeeks A Computer Science portal for Geeks"; 

        minMaxLengthWords(a); 

        // to take input in string use getline(cin, a); 
        System.out.print("Minimum length word: "
                + minWord 
                + "\nMaximum length word: "
                + maxWord); 
    } 
} 

**

Input : "GeeksforGeeks A computer Science portal for Geeks"
Output : Minimum length word: A
         Maximum length word: GeeksforGeeks

**

答案 5 :(得分:0)

你可以试试,

  String s="Today is the happiest day of my life";
  String[] word=s.split(" ");
  String rts=" ";
  for(int i=0;i<word.length;i++){
     if(word[i].length()>=rts.length()){
       rts=word[i];
     }
  }
  System.out.println(rts);
  System.out.println(rts.length());

答案 6 :(得分:0)

试试这个。

   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++ )
        {
            if ( word[i].length() > rts.length() )
                rts = word[i];

        }
        System.out.println( rts );
    }

答案 7 :(得分:-1)

for(int i=0;i<word.length;i++){
            for(int j=0;j<word.length;j++){
                if(word[i].length()>=word[j].length()){
                   if(word[j].length()>=rts.length()) {
                      rts=word[j];
                   }
                } else if(word[i].length()>=rts.length()){
                   rts=word[i];
                }

            }
        }