Question

In Java write an application that accepts up to 20 Strings, or fewer if the user...

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.

Homework Answers

Answer #1

/*
 * 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();
  }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
THIS QUESTION HAS TWO PARTS: A) Write an application that accepts three Strings from the user...
THIS QUESTION HAS TWO PARTS: A) Write an application that accepts three Strings from the user and displays one of two messages depending on whether the user entered the Strings in alphabetical order without regard to case. Save the file as Alphabetize.java. B) Write an application that accepts three Strings from the user and displays them in alphabetical order without regard to case. Save the file as Alphabetize2.java
IN JAVA 1. Write up a small program that accepts two integers from the user. Print...
IN JAVA 1. Write up a small program that accepts two integers from the user. Print which of the two values is bigger. If they are the same, print that they are the same. 2. Write a method that accepts three doubles. Calculate and return the average of the three passed double values. 3. Write up a method that accepts a score between 0-10. Print out a message according to the following table. You ay personally decide the boundaries. i.e.,...
Write a Java application a String input by the user and shows whether the string contains...
Write a Java application a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. needs, at minimum, asl user to input for the String, then...
Write a Java application with a JavaFX GUI that takes a String input by the user...
Write a Java application with a JavaFX GUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum,...
C++ Visual Studio 2019 Write a C++ console application that accepts up to 5 numbers from...
C++ Visual Studio 2019 Write a C++ console application that accepts up to 5 numbers from the user. Display all numbers, the highest, the lowest, and the average of the numbers. Ask the user if they want to continue entering another set of numbers. 1) Use proper naming conventions for your variables and functions. 2) Tell the user what the program is all about. Do NOT start the program with “Enter a number”!! 3) Create an array to store the...
You've been hired by Encoding Egrets to write a C++ console application that encodes and decodes...
You've been hired by Encoding Egrets to write a C++ console application that encodes and decodes text. Use a validation loop to prompt for and get from the user a string that is up to ten characters in length. Create value function encodedString that takes the string as input and returns the equivalent encoded string. Start with an empty encoded string. Use a for loop to encode each character of the input string. Use the following algorithm to encode a...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
The Problem Write a C or C++ program which performs specific operations on bits. The user...
The Problem Write a C or C++ program which performs specific operations on bits. The user will specify a value The user can then set, test, or toggle individual bits in the initial value. Input Your program will prompt the user for: An inital value in hexadecimal. This value may be up to 32 bits (eight hexadecimal digits) long. If the user-supplied value is less than eight characters in length, assume the additional digits are zero and are on the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT