Question

In: Computer Science

Write an application that accepts up to 20 Strings, or fewer if the user enters the...

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()
}

Solutions

Expert Solution

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));
            }
        }
    }
}


Related Solutions

Write a progrm that accepts two strings as input, then indicates if the two strings are...
Write a progrm that accepts two strings as input, then indicates if the two strings are equal when the case of individual letters is ignored. Sample runs are shown below. Use C++ Enter two strings. String 1: PROgraM String 2: proGram PROgraM and proGram are equal. Enter two strings. String 1: litter String 2: LittLe litter and LittLe are not equal.
Write an application that allows a user to enter the names and birthdates of up to...
Write an application that allows a user to enter the names and birthdates of up to 10 friends. Continue to prompt the user for names and birthdates until the user enters the sentinel value ZZZ for a name or has entered 10 names, whichever comes first. When the user is finished entering names, produce a count of how many names were entered, and then display the names. In a loop, continuously ask the user to type one of the names...
Design JavaFX application with 7 labels and one textfield where user enters input inches.  When user enters...
Design JavaFX application with 7 labels and one textfield where user enters input inches.  When user enters his choice and presses enter key to complete input, program outputs resulting yards, feet, and inches.   Use class P5 that extends Application  with start method in it, and class P5Pane that extend GridPane. The only inctance variables for P5Pane class are inputInches where user enters input  inches, and three labels: outYards, outFeet, and outInches where program displays result of conversion.  Use the following names for instance variables:...
write both non-recursive and recursive functions that take the strings from the user and display strings...
write both non-recursive and recursive functions that take the strings from the user and display strings in backwards in python
Write code for a short method that does the following: accepts two strings as parameters, first...
Write code for a short method that does the following: accepts two strings as parameters, first name, and last name; Outputs the following message, concatenated together in one line of output:
Write a C program that prompts the user to enter some information about up to 20...
Write a C program that prompts the user to enter some information about up to 20 individuals (think of a way to welcome and prompt the user). It must be stored in a structure. Once data is entered, the program output it as shown in sample run below. Your program should include a structure with a tag name of: “information”. It should contain the following data as members: a struct to store employee's name, defined as: struct name fullname e.g....
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log in" AccessException: "Your account is forbidden from logging into this server" Any other exception: "Unknown...
Write C++ code that prompts the user for a username and password (strings), and it calls...
Write C++ code that prompts the user for a username and password (strings), and it calls the login() function with the username and password as arguments. You may assume that username and password have been declared elsewhere. Use virtual functions, pure virtual functions, templates, and exceptions wherever possible. Please explain the code. Your code should print error messages if login() generates an exception: LoginException: "Username not found or password incorrect" ServerException: "The login server is busy and you cannot log...
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT