Write an application that accepts up to 20 Strings. Divide them into two lists—o
ID: 3685408 • Letter: W
Question
Write an application that accepts up to 20 Strings. Divide them into two lists—one for short Strings that are five characters or fewer, and the other for long Strings. After data entry is complete, prompt the user to enter which type of String to display, and then output the correct list. If there are no Strings in a requested list, then output an appropriate message. Save the file as DivideStrings.java.
Again, don’t forget to create the application/project DivideStringsTest.java Class that has the main method and an object to use the DivideStrings class.
Explanation / Answer
DivideStrings.java
package org.students;
import java.util.ArrayList;
import java.util.Scanner;
public class DivideStrings {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList();
ArrayList<String> al1 = new ArrayList();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 20; i++) {
System.out.print("Enter a String " + (i + 1) + " :");
String str = sc.nextLine();
if (str.length() <= 5) {
al.add(str);
} else {
al1.add(str);
}
}
System.out.println("The Short Strings You Entered Are ::");
for (String s : al) {
if (s.isEmpty()) {
System.out.println(":: YOU DIDNT ENTERED ANY SHORT STRINGS ::");
} else {
System.out.println(s);
}
}
System.out.println(" ");
System.out.println("The Long Strings You Entered Are ::");
for (String s : al1) {
if (s.isEmpty()) {
System.out.println(":: YOU DIDNT ENTERED ANY LONG STRINGS ::");
} else {
System.out.println(s);
}
}
}
}
__________________________________________________________________________________________
output:
Enter a String 1 :hello
Enter a String 2 :abc
Enter a String 3 :tony
Enter a String 4 :williams
Enter a String 5 :mother
Enter a String 6 :sachin tendulkar
Enter a String 7 :obama
Enter a String 8 :son
Enter a String 9 :killer
Enter a String 10 :strom
Enter a String 11 :remember
Enter a String 12 :terminator
Enter a String 13 :lose
Enter a String 14 :road
Enter a String 15 :fun
Enter a String 16 :tony
Enter a String 17 :ladies
Enter a String 18 :villa
Enter a String 19 :house
Enter a String 20 :nature
The Short Strings You Entered Are ::
hello
abc
tony
obama
son
strom
lose
road
fun
tony
villa
house
The Long Strings You Entered Are ::
williams
mother
sachin tendulkar
killer
remember
terminator
ladies
nature
______________________________________________________________________________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.