Ben
Ben

Reputation: 11

parsing Strings in java

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);
          }
    }
}

Upvotes: 1

Views: 598

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

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);

Upvotes: 1

Related Questions