Answer to Question #161649 in Java | JSP | JSF for aj

Question #161649

Write a JAVA Program that would: Allow user to enter the size and element of a string array. Display the longest string found in the array.



1
Expert's answer
2021-02-07T16:51:14-0500
import java.util.Scanner;
import java.util.stream.Stream;

import static java.util.Comparator.comparingInt;

public class ArrayInputExample1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of elements: ");
        int numberOfElements = scanner.nextInt();
        String[] arrayOfStrings = new String[numberOfElements];
        System.out.println("Enter the elements of the array: ");
        for (int i = 0; i < numberOfElements; i++) {
            arrayOfStrings[i] = scanner.next();
        }
        System.out.print("The longest string in the array is: " + Stream.of(arrayOfStrings).max(comparingInt(String::length)).orElse("Cannot find the longest one"));
    }
}  

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog