Question

Warm up: Parsing strings

5.9 LAB: Warm up: Parsing strings

(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


0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
import java.util.Scanner;

public class ParseStrings {
   
   public static void main(String[] args) {
      
      Scanner scnr = new Scanner(System.in);
      String lineString = "";
      int commaLocation = 0;
      String firstWord = "";
      String secondWord = "";
      boolean inputDone = false;
      
      while (!inputDone) {
         
         System.out.println("Enter input string:");
         lineString = scnr.nextLine();
         
         if (lineString.equals("q")) {
            
            inputDone = true;
            
         }
         
         else if (!lineString.contains(",")) {
            
            System.out.println("Error: No comma in string.\n");
            continue;
            
         }
         
         else {
            
            commaLocation = lineString.indexOf(',');
            firstWord = lineString.substring(0, commaLocation);
            secondWord = lineString.substring(commaLocation + 1, lineString.length());
            
            firstWord = firstWord.trim().replaceAll(" +", " ");
            secondWord = secondWord.trim().replaceAll(" +", " ");
            
            System.out.println("First word: " + firstWord);
            System.out.println("Second word: " + secondWord);
            System.out.println();
            
         }
         
      }
      
      return;
      
   }
   
}


Add a comment
Know the answer?
Add Answer to:
Warm up: Parsing strings
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Python 9.13 LAB: Warm up: Parsing strings (1) Prompt the user for a string that contains two strings separated by a comm...

    Python 9.13 LAB: Warm up: Parsing strings (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...

  • 6.6 Warm up: Parsing strings (Python 3) (1) Prompt the user for a string that contains...

    6.6 Warm up: Parsing strings (Python 3) (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...

  • This code is not working while True: names = input("Enter input string:") if names == "q":...

    This code is not working while True: names = input("Enter input string:") if names == "q": break validlnput ="true" while "," not in names: validlnput="true" print ("Error: No comma in string.") names = input("Enter input string:") # Check for q to terminate if names == "q": exit(0) #set the flag else: validlnput="false" s = names.split(",") print ("First word:",s[0].strip()) print ("Second word:",s[1].strip()) print ("\n") The output should read Enter input string: Jill, Allen First word: Jill Second word: Allen Enter input...

  • Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str...

    Code is wrong: python parsing warm up. while(True): inp_str = input('Enter input string: ') if (inp_str == 'q'): break; while inp_str.find(",")==-1: print('Error: No comma in string. ') inp_str=input('Enter input string: ')       s=inp_str.split(",") print('First word:'+ s[0].strip()) print('Second word:'+ s[1].strip()) print('\n') This is my code for a problem but the output is wrong and I dont know how to fix it (the top is the output im getting the bottom is what i am supposed to get) Enter input string:...

  • In this assignment you are going to handle some basic input operations including validation and manipulation,...

    In this assignment you are going to handle some basic input operations including validation and manipulation, and then some output operations to take some data and format it in a way that's presentable (i.e. readable to human eyes). Functions that you will need to use: getline(istream&, string&) This function allows you to get input for strings, including spaces. It reads characters up to a newline character (for user input, this would be when the "enter" key is pressed). The first...

  • C++ please! (1) Prompt the user to enter the name of the input file. The file...

    C++ please! (1) Prompt the user to enter the name of the input file. The file will contain a text on a single line. Store the text in a string. Output the string. Ex: Enter file name: input1.txt File content: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue! (2) Implement a PrintMenu() function,...

  • Warm up: Online shopping cart (Part 1)

    8.6 LAB*: Warm up: Online shopping cart (Part 1)(1) Create two files to submit:ItemToPurchase.java - Class definitionShoppingCartPrinter.java - Contains main() methodBuild the ItemToPurchase class with the following specifications:Private fieldsString itemName - Initialized in default constructor to "none"int itemPrice - Initialized in default constructor to 0int itemQuantity - Initialized in default constructor to 0Default constructorPublic member methods (mutators & accessors)setName() & getName() (2 pts)setPrice() & getPrice() (2 pts)setQuantity() & getQuantity() (2 pts)(2) In main(), prompt the user for two items and create...

  • Program: Data visualization

    5.10 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they...

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • computer science . the programming language is python

    11.9 LAB*: Program: Data visualization(1) Prompt the user for a title for data. Output the title. (1 pt)Ex:Enter a title for the data: Number of Novels Authored You entered: Number of Novels Authored(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)Ex:Enter the column 1 header: Author name You entered: Author name Enter the column 2 header: Number of novels You entered: Number of novels(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT