Nexus_Valentine
Nexus_Valentine

Reputation: 158

How to only allow a char input from the user and to validate?

In my code below I am trying to get the user to enter 3 separate inputs of 3 letters only. However I cannot find a way to get the scanner to only take a single character and to validate for a-zA-Z. Any help is much appreciated,

Scanner scnObj = new Scanner(System.in);
            System.out.println("enter 3 letters");
            char input1;
            do {
                input1 = scnObj.next(".").charAt(0);
                if ((!Pattern.compile("[a-zA-Z]*").matcher(input1).matches())) {
                    System.out.println("Please try again.");
                }
            } while (!Pattern.compile("[a-zA-Z]*").matcher(input1).matches());

            System.out.println("enter 2nd letter");

Upvotes: 0

Views: 378

Answers (4)

WJS
WJS

Reputation: 40044

You can use System.in directly.

      List<Character> chars = new ArrayList<>();
      try {
         // do until chars contains required number of characters
         while (chars.size() < 3) {
            // read in a character
            char ch = (char) System.in.read();
            // ignore EOL delimiters
            if (ch == '\r' || ch == '\n') {
               continue;
            }
            // if not a letter, clear input buffer and
            // let the user know.
            if (!Character.isLetter(ch)) {
               int cnt = System.in.available();
               System.in.readNBytes(cnt);
               System.out.println("Invalid character encountered, please try again.");
            } else {
              chars.add(ch);
            }
         }
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      System.out.println(chars);

Upvotes: 0

Harihar Das
Harihar Das

Reputation: 494

You can also pass the required regex pattern to the Scanner.next() method. An example:

    Scanner scnObj = new Scanner(System.in);
    System.out.println("enter 3 letters");
    List<Character> inputCharacters = new ArrayList<>(3);
    String input1;
    do {
        try {
            System.out.println("Enter letter no " + (inputCharacters.size() + 1));
            input1 = scnObj.next("[a-zA-Z]");
            inputCharacters.add(input1.charAt(0));
        } catch (Exception nse) {
            scnObj = new Scanner(System.in);
            System.out.println("Please try again.");
        }
    } while (inputCharacters.size() < 3);
    System.out.println("Input characters are " + inputCharacters);

Upvotes: 0

Juan Daniel Ornella
Juan Daniel Ornella

Reputation: 623

Working example:

    Scanner scnObj = new Scanner(System.in);
    String result = "";
    do {
        System.out.println("enter the letter number " + (result.length() + 1));
        char input1 = scnObj.next(".").charAt(0);
        if ((!Pattern.compile("[a-zA-Z]*").matcher(String.valueOf(input1)).matches())) {
            System.out.println("Please try again.");
        } else {
            result += input1;
        }
    } while (result.length() < 3);

    System.out.println("Your result is: " + result);

Upvotes: 0

Renato
Renato

Reputation: 2157

I modified a little your code:

 Scanner scnObj = new Scanner(System.in);
    System.out.println("enter 3 letters");
    String input1 = null;
    do {
        input1 = scnObj.next();
        if ((!Pattern.compile("[a-zA-Z]{1}").matcher(input1).matches())) {
            System.out.println("Please try again.");
        }
    } while (!Pattern.compile("[a-zA-Z]{1}").matcher(input1).matches());

    System.out.println("enter 2nd letter");

Upvotes: 1

Related Questions