In: Computer Science
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.
import java.util.*;
public class CategorizeStrings
{
public static void main (String[] args)
{
// your code here
}
// display()
}
import java.util.*; public class CategorizeStrings { public static void main (String[] args) { ArrayList<String> small = new ArrayList<>(); ArrayList<String> large = new ArrayList<>(); Scanner scanner = new Scanner(System.in); System.out.println("Enter 20 strings (ZZZ to exit):"); String s = scanner.nextLine(); for(int i = 1;i<20 && !s.equals("ZZZ");i++){ if(s.length()<=10){ small.add(s); } else{ large.add(s); } s = scanner.nextLine(); } System.out.print("Which list you want to print(short/long)? "); String choice = scanner.nextLine(); if(choice.equals("short")){ display(small); } else{ display(large); } } public static void display(ArrayList<String> list){ if(list.size() == 0){ System.out.println("The list is empty"); } else { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } }