Home > Mobile >  How can I continue to prompt for input, if my REGEX is not matched?
How can I continue to prompt for input, if my REGEX is not matched?

Time:12-03

I'm trying to get this loop to continue. So far, when input is not matched to my REGEX, "input not valid" gets displayed but loop won't continue. What am I missing here?

Apreciate your help!

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

public static void main(String[] args) {

    String input;
    //some variables
    Pattern pattern = Pattern.compile(REGEX);
    Scanner scn = new Scanner(System.in);


    boolean found = false;
    do {
        System.out.println("ask user for input");
        input = scn.next();
        Matcher matcher = pattern.matcher(input);
        try {
            matcher.find();
            //some Code
            found = true;
            scn.close();
        
        } catch (IllegalStateException e) {
            System.out.println("input not valid."); //stuck here
            scn.next();
            continue;
        }
    } while (!found);

            // some more Code

}
}

CodePudding user response:

There are many problems with your code:

  • IllegalStateException is not raised by the "not-match", but by the Scanner class, so why catch it?
  • you are not doing anything with the result of matcher.find(), I think you want found = matcher.find()
  • in case the input is not valid, you execute twice scn.next();

Moreover:

  • you can simplify the initialization boolean found = false; with boolean found;
  • continue; at the end of a loop is not necessary

Fixed code:

    boolean found;
    do {
        System.out.println("ask user for input");
        input = scn.next();
        found = pattern.matcher(input).find();
        if (!found) {
            System.out.println("input not valid.");
        }
    } while (!found);
    scn.close();

CodePudding user response:

It seems to me like scn.next() after your "input not valid" line isn't doing anything, but it's going to wait for the user to input a string. That's why it looks like the loop isn't continuing: it's waiting for you to input a string because of that line. However, when you do input something, that input will just be thrown away. Removing that line seems like it will do the trick.

CodePudding user response:

Look at your loop the condition of your loop state is !found right once the do finished the while test !found a true is need for continue but you defined found as true so !found= false so the while stop. you should get found to return false till the loop is over

CodePudding user response:

Let's try keeping things simple.

import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern pattern = Pattern.compile("^[a-zA-Z0-9 ] $");

    public static void main(String[] args) {
        Scanner scn = new Scanner(System.in);
        boolean valid;
        String value;
        do {
            System.out.println("ask user for input");
            value = scn.next();
            valid = pattern.matcher(value).matches();
            if (!valid) System.out.println("input not valid.");
        } while (!valid);

        System.out.printf("Valid input is %s", value);
    }
}

The result is:

ask user for input
123-abc
input not valid.
ask user for input
qwerty58
Valid input is qwerty58
Process finished with exit code 0
  • Related