Java Programming: Ch 8 ex 5 a. Write an application that accepts up to 10 String
ID: 3774866 • Letter: J
Question
Java Programming: Ch 8 ex 5
a. Write an application that accepts up to 10 Strings, or fewer if the user enters
a terminating value. Divide the entered Strings into two lists—one for short
Strings that are 10 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. 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 there are no Strings in a requested list, output an appropriate
message. Prompt the user continuously until a sentinel value is entered. Save
the file as CategorizeStrings.java.
b. Modify the CategorizeStrings application to divide the entered Strings into
those that contain no spaces, one space, or more. After data entry is complete,
continuously prompt the user to enter the type of String to display. If the user
does not enter one of the three valid choices, display all of the Strings. Save the
file as CategorizeStrings2.java.
Explanation / Answer
/**
* The java program that prompts user to enter
* until user enters quit or total strings are 10
* then prompts to print short or long strings.
* */
//CategorizeStrings.java
import java.util.Scanner;
public class CategorizeStrings
{
public static void main(String[] args)
{
//Create an instance of Scanner class
Scanner keyboard=new Scanner(System.in);
final int size=10;
int shortCount=0;
int longCount=0;
String userInput="";
int count=0;
String[] shortStrings=new String[size] ;
String[] longStrings=new String[size] ;
System.out.println("Enter string or quit to stop");
System.out.println("Enter string "+(count+1));
userInput=keyboard.nextLine();
//read string until user ents quit to exit
while(!userInput.equals("quit") && count<10)
{
if(userInput.length()>10)
{
longStrings[longCount]=userInput;
longCount++;
}
else
{
shortStrings[shortCount]=userInput;
shortCount++;
}
count=count+1;
System.out.println("Enter string "+(count+1));
userInput=keyboard.nextLine();
}
String userChoice="";
System.out.println("Enter short for short strings or long for long strings");
userChoice=keyboard.nextLine();
//print short strings
if(userChoice.equals("short"))
{
for (int index = 0; index < shortCount; index++)
{
System.out.println(shortStrings[index]);
}
}
else
{
//otherwise print long strings
for (int index = 0; index < longCount; index++)
{
System.out.println(longStrings[index]);
}
}
}
}
------------------------------------------------------------------------------------------------
Output:
Enter string or quit to stop
Enter string 1
one
Enter string 2
two
Enter string 3
three
Enter string 4
four
Enter string 5
javaprogramming
Enter string 6
quit
Enter short for short strings or long for long strings
long
javaprogramming
------------------------------------------------------------------------------------------------
/**
* The java program that prompts user to enter
* until user enters quit or total strings are 10
* then prompts to print strings with single space
* or no space or more sapces.
* */
//CategorizeStrings2.java
import java.util.Scanner;
public class CategorizeStrings2
{
public static void main(String[] args)
{
//Create an instance of Scanner class
Scanner keyboard=new Scanner(System.in);
final int size=10;
String userInput="";
int count=0;
int singleSpaceCount=0;
int noSpaceCount=0;
int moreSpaceCount=0;
String[] singleSpace=new String[size] ;
String[] noStrings=new String[size] ;
String[] moreStrings=new String[size] ;
System.out.println("Enter string or quit to stop");
System.out.println("Enter string "+(count+1));
userInput=keyboard.nextLine();
while(!userInput.equals("quit") && count<10)
{
String[] data=userInput.split(" ");
//check if string contains only two words then single space
if(data.length==2)
{
singleSpace[singleSpaceCount]=userInput;
singleSpaceCount++;
}
//check lenght is one of the string array data then no space
else if(data.length==1)
{
noStrings[noSpaceCount]=userInput;
noSpaceCount++;
}
else
{
//otherwise more spaces
moreStrings[moreSpaceCount]=userInput;
moreSpaceCount++;
}
count=count+1;
System.out.println("Enter string "+count);
userInput=keyboard.nextLine();
}
String userChoice="";
System.out.println("Enter single for single space, nospace for no-space or more for more spaces");
userChoice=keyboard.nextLine();
if(userChoice.equals("single"))
{
for (int index = 0; index < singleSpaceCount; index++)
{
System.out.println(singleSpace[index]);
}
}
else if(userChoice.equals("nospace"))
{
for (int index = 0; index < noSpaceCount; index++)
{
System.out.println(noStrings[index]);
}
}
else if(userChoice.equals("more"))
{
for (int index = 0; index < moreSpaceCount; index++)
{
System.out.println(moreStrings[index]);
}
}
}
}
------------------------------------------------------------------------------------------------
output:
Enter string or quit to stop
Enter string 1
hello world
Enter string 1
hello java
Enter string 2
quit
Enter single for single space, nospace for no-space or more for more spaces
single
hello world
hello java
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.