Question

Program Specifications: Write a method called getString(Scanner keyboard) - This method prompts the user for a...

Program Specifications:

Write a method called getString(Scanner keyboard) - This method prompts the user for a string followed by a carriage return which contains a comma in it. The method returns the string to the main (yes, it's really two strings with a comma separator). You can assume that if the input contains a comma, it also contains two strings.Some examples of strings that can be accepted would be:

Jill, Allen

Jill , Allen

Jill,Allen

Incorporate data validation in getString() - Once you have (1) working, display an error if the input string does not contain a comma. Continue to prompt in the method until the user enters a valid string with a comma.

Write a method called formatAndDisplay(String userString) -add a method called formatAndDisplay(). This method should accept a parameter representing the string entered and extract the two words from the string as well as removing any spaces. The two strings should be stored in two separate variables and then output by the method as indicated in the sample runs below:

(4) Make it loop until user wants to quit - Lastly, extend the program by adding a loop to the main that allows the program to continue until the user enters the character q to quit (it should not be case sensitive, meaning a capital Q should also quit).

Sample Run:

Enter input string:

Jill, Allen

First word: Jill

Second word: Allen

<- This is a blank line

Enter input string:

Jill Allen

Error: No comma in string.

<- This is a blank line

Enter input string:

Harta , Mihart

First word: Harta

Second word: Mihart

<- This is a blank line

Enter input string:

Howie,Luvdat

First word: Howie

Second word: Luvdat

<- This is a blank line

Enter input string:

Mel,Odie

First word: Mel

Second word: Odie

<- This is a blank line

Enter input string:

q

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

public class Main
{
    public static void main(String[] args) throws InterruptedException {
        Scanner scanner= new Scanner(System.in);
        //Loop to repeat the user until presses Q/q
        String userInput,choice;
while (true)
{
    userInput=getString(scanner);

    formatAndDisplay(userInput);
    System.out.print("\nFor Quit press Q/q: ");
     choice = scanner.nextLine();
    if(choice.equalsIgnoreCase("q"))
        break;

}
    }
    /*
    This method prompts the user for a string followed by a carriage return which contains a comma in it.
    The method returns the string to the main
    * */
    private static String getString(Scanner keyboard)
    {
        //Get the input form user
        System.out.print("Enter input String:  ");
        String sentence = keyboard.nextLine();
        //input validation
        /*repeat for prompt until user enter valid string*/
        while (!sentence.contains(","))
        {
            System.out.println("Error: No comma in string.");
            System.out.print("Enter input String:  ");
            sentence= keyboard.nextLine();
        }
        return sentence;
    }
    /*matAndDisplay(String userString) -add a method called formatAndDisplay(). This method should accept a parameter representing the string entered and extract
    the two words from the string as well as removing any spaces. */
    private static void formatAndDisplay(String userString)
    {
        //split the string into two words
        String[] tokens= userString.split(",");
        //Print both words
        System.out.print("First Word: "+tokens[0].trim());
        System.out.print("\nSecond Word: "+tokens[1].trim());
    }
}

//output

//if you need any help regarding this solution ........ please leave a comment ... thanks

Add a comment
Know the answer?
Add Answer to:
Program Specifications: Write a method called getString(Scanner keyboard) - This method prompts the user for a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
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