简体   繁体   中英

Java SE array help needed please

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. Can someone please help me with it and please bear in mind i'm a total newbie in programming and my progress is still just in the basics so try to make it not too complicated for me please. In addition, feel free to pinpoint those redundant codes as I know I have quite a few. :) Thanks!

    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]);


}
    }

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

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM