validate a user input

Viewed 145

Hello I'm new to programming and I'm having a trouble understanding my assignment. I know that this might be a really simple problem for you guys and I'm sorry for that. Is it possible that she's just asking me to write a method that will perform the given instructions?

Write a program to find if the user input is valid base on the instructions.**

  1. a string must have at least nine characters
  2. a string consists of letters and numbers only.
  3. a string must contain at least two digits.
2 Answers

You can simply use the regex, ^(?=(?:\D*\d){2})[a-zA-Z\d]{9,}$ which can be explained as follows:

  • ^ : asserts position at start of a line
  • Positive Lookahead (?=(?:\D*\d){2})
    • Non-capturing group (?:\D*\d){2}
      {2} matches the previous token exactly 2 times
      \D matches any character that's not a digit (equivalent to [^0-9])
      * matches the previous token between zero or more time (greedy)
      \d matches a digit (equivalent to [0-9])
      
  • The pattern, [a-zA-Z\d]{9,} :
    {9,} matches the previous token between 9+ times (greedy)
    a-z matches a single character in a-z
    A-Z matches a single character in A-Z
    \d matches a digit (equivalent to [0-9])
    
  • $ : asserts position at the end of a line

Demo:

import java.util.stream.Stream;

public class Main {
    public static void main(String args[]) {
        //Test
        Stream.of(
                    "helloworld",
                    "hello",
                    "hello12world",
                    "12helloworld",
                    "helloworld12",
                    "123456789",
                    "hello1234",
                    "1234hello",
                    "12345hello",
                    "hello12345"
        ).forEach(s -> System.out.println(s + " => " + isValid(s)));
    }
    static boolean isValid(String s) {
        return s.matches("^(?=(?:\\D*\\d){2})[a-zA-Z\\d]{9,}$");
    }
}

Output:

helloworld => false
hello => false
hello12world => true
12helloworld => true
helloworld12 => true
123456789 => true
hello1234 => true
1234hello => true
12345hello => true
hello12345 => true

Requirement #1: a string must have at least nine characters

This is solved by checking whether the length of the String is greater than 9, with s.length()>9

Requirement #2: a string consists of letters and numbers (whole numbers) only.

Use the regex [a-zA-Z0-9]+, which matches all Latin alphabet characters and numbers.

Requirement #3: a string must contain at least two digits.

I've written a method that loops through every character and uses Character.isDigit() to check whether it is a digit.

Check it out:

public static boolean verify(String s) {
    final String regex = "[a-zA-Z0-9]+";
    System.out.println(numOfDigits(s));
    return s.length() > 9 && s.matches(regex) && numOfDigits(s) > 2;
}

public static int numOfDigits(String s) {
    int a = 0;
    int b = s.length();
    for (int i = 0; i < b; i++) {
        a += (Character.isDigit(s.charAt(i)) ? 1 : 0);
    }
    return a;
}
Related