Find The Longest String In An Array Java

Find The Longest String In An Array Java | In this section, we will see how to find the longest string in an array in Java. We will take the array of strings and find the longest element containing the most characters among the given elements.

See the below example for how to find the longest string in array Java:-
String array = [“Hi”, “Hello”, “How are you?”]
The longest string in the array:- “How are you?”

Program To Find Longest String In An Array Java

In this program, we have created a method called getLongestString() which takes a single parameter called “array”. Here, we have declared max as 0 and string value to null and by using for each loop we iterate through the array elements and find out the maximum length of the element in the array and return the same. Then in the main method, we call this method and print the output.

import java.util.Arrays;

public class Main {

   public static String getLongestString(String[] array) {
      int max = 0;
      String longest = null;
      for (String str : array) {
         if (str.length() > max) {
            max = str.length();
            longest = str;
         }
      }
      return longest;
   }

   public static void main(String[] args) {
      String[] names = 
       { "Apple", "Mango", "Fish", "Honey Bee", "Watermelon" };
      String longestString = getLongestString(names);

      System.out.println("String array: " 
              + Arrays.toString(names));
      System.out.println("Longest string in string array: " 
              + longestString);
   }
}

Output:-

String array: [Apple, Mango, Fish, Honey Bee, Watermelon]
Longest string in string array: Watermelon

In the above program, we have not considered the case when the string is empty or null. In the given string array there is a chance of one element as null in that case the above program will throw NullPointerException. Similarly, if the string array is null or empty then we should return an empty string. Let us see another program to find the longest string in an array Java.

import java.util.Arrays;

public class Main {

   public static String getLongestString(String[] word) {
      if (word == null || word.length < 1) {
         return "";
      }
      String longestString = word[0];
      for (int i = 1; i < word.length; i++) {
         if (word[i] != null && 
             word[i].length() > longestString.length()) {
            longestString = word[i];
         }
      }
      return longestString;
   }

   public static void main(String[] args) {
      String[] names = 
         { "Apple", "Mango", "Fish", 
           "Honey Bee", "Watermelon", "", null };
      String longestString = getLongestString(names);

      System.out.println("String array: " 
                        + Arrays.toString(names));
      System.out.println("Longest string in string array: " 
                        + longestString);
   }
}

Output:-

String array: [Apple, Mango, Fish, Honey Bee, Watermelon, , null]
Longest string in string array: Watermelon

In these programs we have taken the Arrays.toString() method to display the string array. Arrays class of java.util package also contains many other methods for the array-like sorting, searching, copying, and e.t.c.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *