santosh-patil
santosh-patil

Reputation: 1550

input Pattern matching java

I want to write a function validate() which will take some pattern or regular expression as argument and will ask user to input its choice. If the choice matches the pattern, it will return the choice, otherwise it will ask user to re-enter the choice.

For example if I call validate() with 123 as argument, it will return either 1 or 2 or 3 depending upon user input.

But I don't know how patterns or regular expressions are used. Please help.

I have written some code but I am not sure what to write at a couple of places. I want the validate function written below to accept the input either 1 or 2 or 3 and return the same.

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Pat
{
  public static void main(String args[])
  {
    int num=validate(Pattern.compile("123"));//I don't know whether this is right or not
    System.out.println(num);
  }
  static int validate(Pattern pattern)
  {
    int input;
    boolean validInput=false;
    do
    {
      try
      {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        input=Integer.parseInt(br.readLine());
        validInput=true;
      }catch(Exception e)
      {
        System.out.println(""+e);
      }
    }while(!validInput || input.matches(pattern));// This also seems like a problem.
    return input;
  }
}

Upvotes: 1

Views: 7222

Answers (2)

Boro
Boro

Reputation: 7943

I think you meant to enter your pattern as "[123]".

You nearly solved it yourself mate. :)

Plus I have noticed few things there you should reconsider. This is your code after my edits. Enjoy, hope it does what you were after.

import java.io.*;


class Pat
{
    public static void main(String args[])
    {
        int num = validate("[123]");
        System.out.println(num);
    }

    static int validate(String pattern)
    {
        String input = "";
        boolean validInput = false;
        do
        {
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                input = br.readLine();
                if(input.matches(pattern))
                    validInput = true;
            }catch(Exception e)
            {
                System.out.println("" + e);
            }
        }while(!validInput);
        return Integer.parseInt(input);
    }
}

Oi, Boro.

Upvotes: 2

Jason
Jason

Reputation: 674

If you don't want to use a pattern matcher, you can just check to see if the input is one of the characters in your string of options.

public class Main {
public static void main(String[] args) {
    String options = "123abc";
    System.out.println("You chose option: " + validate(options));
}

static String validate(String options)
{
    boolean validInput=false;
    String input = "";
    do {
        System.out.println("Enter one of the following: " + options);
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        try {
            input = br.readLine();
            if (input.length() == 1 && options.indexOf(input) >= 0) {
              validInput=true;
            }
        } catch (IOException ex) {
            // bad input
        }
    } while(!validInput);
    return input;
} }

Upvotes: 0

Related Questions