Respuesta :
Answer:
Here is the JAVA code:
import java.util.Scanner; // to get input from user
public class ParsingStrings //class ParsingStrings
{ public static void main(String[] args) { //start of main() function body
// creates object of Scanner class
Scanner scnr = new Scanner(System.in);
Scanner inputstream; // to hold the inputline
String inputline = ""; //contains the string entered by user
String substring1 = ""; //First word of the input string
String substring2 = ""; // next word of the input string after substring1
System.out.println("Enter input string: ");
//prompts user to enter a string like Allen, Jill
while (inputline.matches("q") == false) {
//while loop keeps reading the input strings until user enters q to exit
inputline = scnr.nextLine(); //reads and scans the input string
inputline = inputline.replaceAll(",",", "); // to replace all commas to " ,"
inputstream = new Scanner(inputline); //scans input string
int commaPos = inputline.indexOf(",");
// indexOf() returns the position of comma (",")
//if the string does not have comma and user does not enter q to exit
if ((commaPos<=-1) && (inputline.matches("q") == false)) {
//display error message as comma is not found in input string
System.out.println("Error: No comma in string");
System.out.println("Enter input string: "); }
//ask user to enter string again until he enters valid comma separated string
//if user enters no string, or one word or no comma seperated 2 words
else if ((commaPos <= -1) && (inputline.length() == 0 || inputline.split("\\s+").length < 2) && (inputline.matches("q") == false)) {
//ask user to enter two words
System.out.println("Please enter two words");
System.out.println("Enter input string: "); }
// checks if user has entered q
else if (inputline.matches("q") == false) {
substring1 = inputstream.next(); //stores word 1
substring2 = inputstream.nextLine(); //stores word 2
//prints the first word by replacing empty spaces with comma
System.out.println("First word: " + substring1.replaceAll("\\s",""));
//prints second word by removing empty spaces
System.out.println("Second word: " + substring2.replaceAll("\\s","").replaceAll("\\W",""));
System.out.println("\n");
// asks user to enter string until user types q
System.out.println("Enter input string: "); } } } }
Explanation:
The program is briefly explained in the comments given in the code.
The program takes input from user in the form of strings.
The string contains two sub strings separated by comma.
commaPos stores the position of comma in the input string using indexOf() function.
If and else if conditions used here checks the input string for the following:
- The strings should be separated by commas. commaPos <= -1
- The user does not enter just one word. inputline.split("\\s+").length < 2. split() function here splits the input string by empty space between both the words and then checks the length that it should not be less than 2 which means two words should be entered.
- The user should enter a string.
- The length of the input string should not be 0. inputline.length() == 0
- replaceAll() function is used to remove the spaces and extra commas and the other non word characters and replace them with comma so that the two words can easily be separated by a single comma and can be displayed in output.
The screenshot of output of the program is attached.

In this exercise we have to use the knowledge in computational language in JAVA to write the following code:
We have the code can be found in the attached image.
So in an easier way we have that the code is
import java.util.Scanner;
System.out.println("Enter input string: "); }
else if ((commaPos <= -1) && (inputline.length() == 0 || inputline.split("\\s+").length < 2) && (inputline.matches("q") == false)) {
System.out.println("Please enter two words");
substring2 = inputstream.nextLine(); //stores word 2
System.out.println("First word: " + substring1.replaceAll("\\s",""));
System.out.println("Second word: " + substring2.replaceAll("\\s","").replaceAll("\\W",""));
System.out.println("\n");
System.out.println("Enter input string: "); } } } }
See more about JAVA at brainly.com/question/12975450
