1

The same question was asked before but I the help wasn't sufficient enough for me to get it solved. When I run the program many times, it goes well for a string with comma in between(e.g. Washington,DC ). For a string without comma(e.g. Washington DC) the program is expected to print an error message to the screen and prompt the user to enter the correct input again. Yes, it does for the first run. However, on the second and so run, it fails and my suspect is on the while loop.

Console snapshot: Enter input string: Washington DC =>first time entered & printed the following two lines

Error: No comma in string.

Enter input string: Washington DC => second time, no printouts following i.e failed

Here's my attempt seeking your help.

public class practice {

     public static void main(String[] args) {

         Scanner scnr = new Scanner(System.in);
          String userInput = "";
          String delimit =",";
         boolean inputString = false;

             System.out.println("Enter input string:");
             userInput = scnr.nextLine();
          while (!inputString) {

             if (userInput.contains(delimit)){
                String[] userArray = userInput.split(delimit);
               // for(int i=0; i<userArray.length-1; i++){
                System.out.println("First word: " + userArray[0]); //space
                System.out.println("Second word:" + userArray[1]);
                System.out.println();               
                //}
           }

             else if (!userInput.contains(delimit)){
                System.out.println("Error: No comma in string.");
               inputString= true;
             }           
             System.out.println("Enter input string:");
             userInput = scnr.nextLine();
            while(inputString);
          }
    }
}

1 Answer 1

1

You can easily solve this problem using a simple regex ^[A-Z][A-Za-z]+, [A-Z][A-Za-z]+$

So you can check the input using :

boolean check = str.matches("^[A-Z][A-Za-z]+, [A-Z][A-Za-z]+$");

Then you can use do{}while() loop instead, so your code should look like this :

public static void main(String[] args) {

    Scanner scnr = new Scanner(System.in);
    String userInput;

    do {
        System.out.println("Enter input string:");
        userInput = scnr.nextLine();
    } while (!userInput.matches("^[A-Z][A-Za-z]+, [A-Z][A-Za-z]+$"));
}

regex demo


Solution 2

...But I can't apply regex at this time and I wish others help me to finish up the work the way I set it up

In this case you can use do{}while(); like this :

Scanner scnr = new Scanner(System.in);
String userInput;
String delimit = ",";
boolean inputString = false;
do {
    System.out.println("Enter input string:");
    userInput = scnr.nextLine();
    if (userInput.contains(delimit)) {
        String[] userArray = userInput.split(delimit);
        System.out.println("First word: " + userArray[0]);
        System.out.println("Second word:" + userArray[1]);
        System.out.println();
    } else if (!userInput.contains(delimit)) {
        System.out.println("Error: No comma in string.");
        inputString = true;
    }
} while (inputString);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your swift reply and help, appreciated! But I can't apply regex at this time and I wish others help me to finish up the work the way I set it up
Awesome, that's helpful. With adding System.out.println("Enter input string:"); right below the while loop prompts the user again to enter a string input and works perfect. thanks!
you blocked me for asking for help. I wish I would know how glad you're by making this on me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.