0

I need some help here with my Java school work. We were told to prompt the user for five words and from there determine the longest word of them and print to console the longest word as well as the number of characters in it.

Right now, I only manage to sort them out using the arrays by displaying the longest number of characters but I'm not sure how to display the word itself. I'm a total beginner in programming and my progress is still just in the basics. What can I try next?

    import java.util.Scanner;
    import java.util.Arrays;

    class LongestWord
    {
public static void main(String [] args)
{       
    Scanner theInput = new Scanner(System.in);
    System.out.println("Please enter your five words");
    
    String fWord = theInput.next();
    String sWord = theInput.next();
    String tWord = theInput.next();
    String fhWord = theInput.next();
    String ffWord = theInput.next();
    
    System.out.println(fWord + sWord + tWord + fhWord + ffWord);
    
    int [] wordCount = new int[5];
    
    wordCount[0] = fWord.length();
    wordCount[1] = sWord.length();
    wordCount[2] = tWord.length();
    wordCount[3] = fhWord.length();
    wordCount[4] = ffWord.length();
    
    Arrays.sort(wordCount);
    
    System.out.println(wordCount[4]);
    

}
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
alan_ogz83
  • 73
  • 1
  • 2
  • 10
  • 1
    You don't need to sort just to find the longest word nor do you need to store all words in memory. Read them one by one and only store the up to now longest word. – Henry Jun 25 '14 at 06:22
  • 1
    You are doing good, just think a little more and you should be able to code it yourself ! – Abubakkar Jun 25 '14 at 06:22

3 Answers3

3

You need to add all the string to array and iterate all of them.

sample:

    String [] wordCount = new String[5];

    wordCount[0] = fWord;
    wordCount[1] = sWord;
    wordCount[2] = tWord;
    wordCount[3] = fhWord;
    wordCount[4] = ffWord;

    String longest = "";

    longest = wordCount[0]; //get the first array of words for checking

    for(String s : wordCount) //iterate to all the array of words
    {
        if(longest.length() < s.length()) //check if the last longest word is greater than the current workd
            longest = s; //if the current word is longer then make it the longest word
    }

    System.out.println("Longest Word: " + longest + " lenght: " + longest.length());

result:

Please enter your five words
12345
1234
123
12
1
123451234123121
Longest Word: 12345 lenght: 5
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Hey hi Rod, thanks for your help but I still couldn't get it. this part where you said for(String s : wordCount), when i compile, it says required string found int, incompatible types. thanks! – alan_ogz83 Jun 25 '14 at 06:36
  • thanks Rod but it still couldn't compile. right now it still says incompatible types required string found int but it states at the area where you typed wordCount[0] = fWord.length(); onwards. :) – alan_ogz83 Jun 25 '14 at 06:48
  • @alan_ogz83 editted just remove the lenght() see above – Rod_Algonquin Jun 25 '14 at 06:49
  • oh thanks so much Rod, so far for the for loop it seems a bit too complex for me to understand but i will go research about it. once again, thanks for your patience and help. ;) – alan_ogz83 Jun 25 '14 at 06:53
  • yeah mate it helps a lot. did i do the correct thing? I clicked on the up arrow as well as the tick? – alan_ogz83 Jun 25 '14 at 06:56
  • hey hi Rod, I have another question relating to this school work and was wondering if you can advise me on that? Thanks so much again! :) http://stackoverflow.com/questions/24425293/java-se-vector-array-help-please – alan_ogz83 Jun 26 '14 at 08:01
2

You need to store all words into array and get the maximum value after sort according to its length.

 String[] words = ....//Store all words into this array.

 Arrays.sort(words, new Comparator<String>() {

         @Override
         public int compare(String o1, String o2) {
             return o2.length() - o1.length();
         }
     });

 System.out.println(words[0]);

or, if you use java-8 than you will get the result more easily,

String longWord=
       Arrays.stream(words).max((o1, o2)->o1.length()-o2.length()).get();
Masudul
  • 21,823
  • 5
  • 43
  • 58
0

Instead of putting lengths into an array, you should put all the words in an array and then loop them using for/while and check length of each string comparing with the previous one to record the max length string.

Or another way may be to read strings using loop and you can perform same logic of comparing lengths without using additional array.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33