This is the question I am stuck on -
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.
This is the code I have but I am not getting any checks correct with it -
import java.util.Scanner;
public class CategorizeStrings {
public static void main(String[] args) {
int SIZE = 20;
String[] sStrings = new String[SIZE];
String[] lStrings = new String[SIZE];
String s;
int sCounter = 0, lCounter = 0;
Scanner sc = new Scanner(System.in);
for (int i = 0; i < SIZE; i++) {
System.out.print("Enter a string : ");
s = sc.nextLine();
if (s.equalsIgnoreCase("zzz")) { // checking for sentinal value
break;
} else {
if (s.length() <= 10) // if short string
sStrings[sCounter++] = s;
else // if long string
lStrings[lCounter++] = s;
}
} // end of for
System.out.println("\n\n1.Display Short strings\n2.Display Long Strings\nEnter choice : ");
int choice = sc.nextInt();
if (choice == 1) {
if (sCounter == 0) {
System.out.println("The list is empty");
} else {
for (int i = 0; i < sCounter; i++) {
System.out.println(sStrings[i]);
}
}
} else if (choice == 2) {
if (lCounter == 0) {
System.out.println("The list is empty");
} else {
for (int i = 0; i < lCounter; i++) {
System.out.println(lStrings[i]);
}
}
} else {
System.out.println("Invalid Choice");
}
}
}
Here is an example of one of the checks I a,m gettign wrong -
Test Case Incomplete
Categorize Strings A B C D, 3 idioms
Input
A
B
C
D
A penny for your thoughts
Barking up the wrong tree
Curiosity killed the cat
ZZZ
S
ZZZ
Output
Enter a string : A
Enter a string : B
Enter a string : C
Enter a string : D
Enter a string : A penny for your thoughts
Enter a string : Barking up the wrong tree
Enter a string : Curiosity killed the cat
Enter a string : ZZZ
1.Display Short strings
2.Display Long Strings
Enter choice :
S
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:864) at java.util.Scanner.next(Scanner.java:1485) at java.util.Scanner.nextInt(Scanner.java:2117) at java.util.Scanner.nextInt(Scanner.java:2076) at CategorizeStrings.main(CategorizeStrings.java:45)
ZZZ
Results(all are marked in red for being wrong/incorrect)
A