5.6 Warm up: Parsing strings (Java) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen...


5.6 Warm up: Parsing strings (Java)


(1) Prompt the user for a string that contains two strings separated by a comma. (1 pt)


Examples of strings that can be accepted:


Jill, Allen


Jill , Allen


Jill,Allen


Ex:


Enter input string: Jill, Allen





(2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered.Note: If the input contains a comma, then assume that the input also contains two strings. (2 pts)



Ex:


Enter input string: Jill Allen


Error: No comma in string


Enter input string: Jill, Allen





(3) Extract the two words from the input string and remove any spaces. Store the strings in two separate variables and output the strings. (2 pts)



Ex:


Enter input string: Jill, Allen


First word: Jill


Second word: Allen





(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)



Ex:


Enter input string: Jill, Allen


First word: Jill


Second word: Allen




Enter input string: Golden , Monkey


First word: Golden


Second word: Monkey




Enter input string: Washington,DC


First word: Washington


Second word: DC




Enter input string: q


Here is my code:


import java.util.Scanner;


public class ParseStrings {

public static void main(String[] args) {

/*Type your code here. */

Scanner scnr = new Scanner(System.in);

String userInput = " ";

boolean inputDone = false;


while (!inputDone) {

System.out.print("Enter input string: \n");

userInput = scnr.nextLine();



if(userInput.equals("q")) {

System.out.println("First word:" + userInput);

inputDone = true;

} else {

String[] userArray = userInput.split(",");

System.out.println("First word: " + userArray[0]);

System.out.println("Second word:" + userArray[1]);

System.out.println();

System.out.println();

}

}


return;

}

}


Output:


1. Compare output


1/1














Input



Jill, Allen q



Your output correctly starts with



Enter input string:



2. Compare output


0/2


















Input



Jill Allen JillAllen Jill Allen q



Your output starts with



Enter input string: First word: Jill Allen Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at ParseStrings.main(ParseStrings.java:20)



Expected output starts with



Enter input string: Error: No comma in string Enter input string: Error: No comma in string Enter input string: Error: No comma in string Enter input string:



3. Compare output


2/2














Input



Jill, Allen q



Your output correctly starts with



Enter input string: First word: Jill Second word: Allen



4. Compare output


0/1


















Input



Jill, Allen Golden , Monkey Washington,DC q



Your output



Enter input string: First word: Jill Second word: Allen Enter input string: First word: Golden Second word: Monkey Enter input string: First word: Washington Second word:DC Enter input string: First word:q



Expected output



Enter input string: First word: Jill Second word: Allen Enter input string: First word: Golden Second word: Monkey Enter input string: First word: Washington Second word: DC Enter input string:



5. Compare output


0/1


















Input



Clarke , Geffen Signals,Systems Macintosh, Apple q



Your output



Enter input string: First word: Clarke Second word: Geffen Enter input string: First word: Signals Second word:Systems Enter input string: First word: Macintosh Second word: Apple Enter input string: First word:q



Expected output



Enter input string: First word: Clarke Second word: Geffen Enter input string: First word: Signals Second word: Systems Enter input string: First word: Macintosh Second word: Apple Enter input string:




May 19, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here