Please Read All Java Question Warm up: Parsing strings (Java) (1) Prompt the use
ID: 3777235 • Letter: P
Question
Please Read All
Java Question
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:
(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:
(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:
(4) Using a loop, extend the program to handle multiple lines of input. Continue until the user enters q to quit. (2 pts)
Ex:
*********************************************************************************
Here is the code being used:
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ParseStrings {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) { //infinte loop
System.out.println("Enter input string:");
String input = br.readLine(); //read the input
if(input.equalsIgnoreCase("q")) //if user inputs q, then exit the loop
break;
if(input.indexOf(",") == -1){ //if comma is not found in the user input
System.out.println("Error: No comma in string ");
continue;
}
String[] words = input.split(","); //if user not entered q, and also comma is present, then split the input based on comma
String firstWord = words[0].trim(); //get the first word and remove the unwanted spaces
String secondWord = words[1].trim(); //get the second word and remove the unwanted spaces
System.out.println();
System.out.println("First word: "+firstWord); //print the words
System.out.println("Second word: "+secondWord);
}
}
}
*********************************************
Here are the errors:
SUBMIT FOR GRADING SUBMITTED: 10:42 AM ON 11/25/16 1. Compare output Jill, Allen nput Enter input string: Your output starts with Fi Expected output starts with Enter input string: 2. Compare output Jill Allen JillAllen. Input Jill Allen Enter input string: Error: No comma in string Enter input string: Your output starts with Error No comma in string Enter input string: Error No comma in string Enter input string: Enter input string: Error No comma in string Enter input string: Expected output starts with Error: No comma in string Enter input string: Error No comma in string Enter input string: 3. Compare output Jill Allen Input Enter input string: J Your output starts with First word: Jill Second word Allen Enter input string Expected output starts with First word: Jill Total: 0/7 01 0/2 0/2Explanation / Answer
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ParseStrings {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) { //infinte loop
// You used println which gave a new line -- Error
System.out.print("Enter input string:");
String input = br.readLine(); //read the input
if(input.equalsIgnoreCase("q")) //if user inputs q, then exit the loop
break;
if(input.indexOf(",") == -1){ //if comma is not found in the user input
// You did put a space after string "No comma in string"
System.out.println("Error: No comma in string");
continue;
}
String[] words = input.split(","); //if user not entered q, and also comma is present, then split the input based on comma
String firstWord = words[0].trim(); //get the first word and remove the unwanted spaces
String secondWord = words[1].trim(); //get the second word and remove the unwanted spaces
System.out.println("First word: "+firstWord); //print the words
System.out.println("Second word: "+secondWord);
}
}
}
/*SAMPLE OUTPUTS
*/
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ParseStrings {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) { //infinte loop
// You used println which gave a new line -- Error
System.out.print("Enter input string:");
String input = br.readLine(); //read the input
if(input.equalsIgnoreCase("q")) //if user inputs q, then exit the loop
break;
if(input.indexOf(",") == -1){ //if comma is not found in the user input
// You did put a space after string "No comma in string"
System.out.println("Error: No comma in string");
continue;
}
String[] words = input.split(","); //if user not entered q, and also comma is present, then split the input based on comma
String firstWord = words[0].trim(); //get the first word and remove the unwanted spaces
String secondWord = words[1].trim(); //get the second word and remove the unwanted spaces
System.out.println("First word: "+firstWord); //print the words
System.out.println("Second word: "+secondWord);
}
}
}
/*SAMPLE OUTPUTS
Enter input string: Jill Allen Error: No comma in string Enter input string: JillAllen Error: No comma in string Enter input string: Jill Allen Error: No comma in string Enter input string: q
Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input string: q
*/