In Java write an application that accepts up to 20 Strings, or fewer if the user enters the terminating value ZZZ. Store each String in one of two lists—one list for short Strings that are 10 characters or fewer and another list for long Strings that are 11 characters or more. After data entry is complete, prompt the user to enter which type of String to display, and then output the correct list.
For this exercise, you can assume that if the user does not request the list of short strings, the user wants the list of long strings. If a requested list has no Strings, output The list is empty. Prompt the user continuously until a sentinel value, ZZZ, is entered.
/* * JAVA Program to distribute strings of differnet lengths */ import java.util.Scanner; class Main { public static void main(String[] args) { int srt = 0, lng = 0, count = 0, choice; String str, sentinel = "ZZZ"; String[] shortStrArr = new String[20]; String[] longStrArr = new String[20]; Scanner input = new Scanner(System.in); System.out.println("Enter strings (ZZZ to terminate) -"); str = input.nextLine(); while (str.equals(sentinel) != true && count < 20) { if (str.length() <= 10) { shortStrArr[srt] = str; srt++; } else { longStrArr[lng] = str; lng++; } str = input.nextLine(); } System.out.println("Which type of strings to display?"); System.out.println("1. Short"); System.out.println("2. Long"); choice = input.nextInt(); if (choice == 1) { System.out.println("Displaying short array -"); for (int i = 0; i < srt; i++) { System.out.println(shortStrArr[i]); } } else { System.out.println("Displaying short array -"); for (int i = 0; i < lng; i++) { System.out.println(longStrArr[i]); } } input.close(); } }
Get Answers For Free
Most questions answered within 1 hours.