Write a complete Java program called \'\'StringMaxAndMinLength\'\' (without the
ID: 3885918 • Letter: W
Question
Write a complete Java program called ''StringMaxAndMinLength'' (without the quotation marks) that does the following: prompts the user with three separate prompts for three strings (be sure to handle spaces in the user-supplied strings) finds the maximum and minimum lengths of the three strings the user entered prints explanatory text that says what strings the user entered, what the maximum length of the three strings is, and what the minimum length of the three strings is. This program should be done *without* using if statements.
Explanation / Answer
StringMaxAndMinLength.java
import java.util.Scanner;
public class StringMaxAndMinLength {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the first string: ");
String s1 = scan.nextLine();
System.out.println("Enter the second string: ");
String s2 = scan.nextLine();
System.out.println("Enter the third string: ");
String s3 = scan.nextLine();
int l1 = s1.length();
int l2 = s2.length();
int l3 = s3.length();
int min, max;
max = (l1 > l2 && l1 > l3) ? l1 : ((l2 > l3) ? l2 : l3);
min = (l1 < l2 && l1 < l3) ? l1 : ((l2 < l3) ? l2 : l3);
System.out.println("The maximum length of the three strings is "+max);
System.out.println("The minimum length of the three strings is "+min);
}
}
Output:
Enter the first string:
Hi
Enter the second string:
Hello
Enter the third string:
Good Morning
The maximum length of the three strings is 12
The minimum length of the three strings is 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.