Hi guys,
I wrote some code for finding the three longest words in a string. The code I wrote seem too complicated, is there a better way to appoarch this?

public class Size {
    public String[] threeLongest(String word){
        String[] three = new String[3];
        String[] words = word.split(" ");
        three[0] = longest(word);
        three[1] = find(words, three[0]);
        three[2] = find(words, three[0], three[1]);
        return three;
    }

    public String longest(String word){
        String[] words = word.split(" ");
        int largestLength = -1;
        String largestWord ="";

        for(String x:words)
            if(x.length()>=largestLength){
                largestWord = x;
                largestLength = x.length();
            }   
        return largestWord;
    }

    private String find(String[] words, String longest){
        String l1 ="";
        int lnum = -1;
        for(String x: words){
            if(!x.equals(longest)){
                if(x.length()>= lnum){
                        l1=x;
                        lnum=x.length();
                }   
            }   
        }
        return l1;
    }

    private String find(String[] words, String l1, String l2){
        String longest ="";
        int lnum = -1;
        for(String x: words){
            if(!x.equals(l1)){
                if(!x.equals(l2))
                    if(x.length()>= lnum){
                            longest=x;
                            lnum=x.length();
                    }   
            }   
        }
        return longest;
    }
}

The easiest solution would simply be to sort the strings by length (using Java's built-in sort method) and then simply take the first three (or last three depending on the order in which you sorted) strings from the sorted array.

That solution would be much shorter, simpler and easier to understand than yours. However it's O(n log n) whereas your solution is O(n) - also if this is Homework, using sort might count as cheating.

To simplify your solution, the first step would be to do it in one single loop, instead of having three separate methods that all look the same except for some adjustments to the looping logic. To do this you should have three variables, keeping track of the longest, second longest and third longest string respectively. Then for each string you simply check whether it's longer than the longest, second longest or third longest string and, if so, adjust the variables accordingly.

commented: Excellent reply +14

Dude, reading a string by length would be much easier. The steps goes this way

  1. Read the length of each word. For every word there will be space compare the string with the space if space found store the word in an array.
  2. Now the words are ready placed in arrays. Read each length of array.
  3. Compare each array length.
  4. Sort them according to increasing or decreasing order.
  5. Print the values of last three or first three depending on the order you give.

Thanks guys, that so much simpler.

Read the length of each word. For every word there will be space compare the string with the space if space found store the word in an array.

How is that better than using the split method to get an array of words like the OP did?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.