Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

follow the instruction please I tried the code somehow it works fine but help is needed how you remove spaces from the input strings and

follow the instruction please I tried the code somehow it works fine but help is needed how you remove spaces from the input strings and store them so that the output just like the way shown below. PLEASE GIVE AN ATTENTION FOR RULE(3) BELOW.


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

  • 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. 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. 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. 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


// PLEASE MODIFY MY CODE PER RULE(3) ABOVE ADD A CODEFOR REMOVING SPACES

 

import java.util.Scanner;
public class ParseStrings {
public static void main(String[] args) {
   Scanner scnr = new Scanner(System.in);
   //loop till it is not terminated by break  
   while(true) {
         System.out.println("Enter input string:");
         String input = scnr.nextLine();
         //if q or Q is entered
         if(input.equalsIgnoreCase("q"))
         {
          break;   //terminate loop
         } 
            
         else {
              //if there is comma in the input string
             if(input.contains(",")) {
                 //split the string by comma and store in string array str
                 String[] str = input.split(",");
                 //display first word at 0 index
                 System.out.println("First word: " + str[0]);
                 //display second word at 1 index
                 System.out.println("Second word:" + str[1]);
                 System.out.println();
             }
             //if string does not contain comma
             else {
                 System.out.println("Error: No comma in string.");
                  System.out.println();
                  
             }
         }
     }
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entrepreneurship

Authors: Andrew Zacharakis, William D Bygrave

5th Edition

1119563097, 9781119563099

More Books

Students also viewed these Databases questions

Question

explain the importance of collecting a random sample with refernces

Answered: 1 week ago

Question

What would the company sponsoring the volunteer gain?

Answered: 1 week ago