Finding Data Type of User Input using Regular Expression in Java

Given a string, the task is to find its corresponding datatype using regular expression in java.

We can broadly classify all data types into following types:

  1. Integer: Numeric datatypes like byte, short, int, long take the form of an Integer object.
  2. Double: Decimal datatypes like float and double take the form of Double object.
  3. Date: Date in any format (like dd-mm-yyyy or dd/mm/yyyy) is a part of java.util.Date
  4. String: All remaining inputs come under the String type.

Note: Character inputs and boolean values will also be considered as string.


Input: “56.73”

Output: java.lang.Double

Explanation: 56.73 is of float data type which are part of java.lang.Double

Input: “true”

Output: java.lang.String

Explanation: Here true is considered as a regular string which is a part of java.lang.String


  • Take input in the form of a string.
  • Now if the input contains only digits, it is an Integer object. If it contains numbers with a decimal point, it is a Double-object. If the input is in the form of a Date, we print it as java.util.Date object. Else, we say that the input is a String object which may contain alphanumeric and special characters.

Below is the implementation of the above approach:






public class GFG {
    // method stub
    public static void main(String[] arg)
        String input = "56.73";
        String dataType = null;
        // checking for Integer
        if (input.matches("\\d+")) {
            dataType = "java.lang.Integer";
        // checking for floating point numbers
        else if (input.matches("\\d*[.]\\d+")) {
            dataType = "java.lang.Double";
        // checking for date format dd/mm/yyyy
        else if (input.matches(
                     "\\d{2}[/]\\d{2}[/]\\d{4}")) {
            dataType = "java.util.Date";
        // checking for date format mm/dd/yyyy
        else if (input.matches(
                     "\\d{2}[/]\\d{2}[/]\\d{4}")) {
            dataType = "java.util.Date";
        // checking for date format dd-mon-yy
        else if (input.matches(
                     "\\d{2}[-]\\w{3}[-]\\d{2}")) {
            dataType = "java.util.Date";
        // checking for date format dd-mon-yyyy
        else if (input.matches(
                     "\\d{2}[-]\\w{3}[-]\\d{4}")) {
            dataType = "java.util.Date";
        // checking for date format dd-month-yy
        else if (input.matches("\\d{2}[-]\\w+[-]\\d{2}")) {
            dataType = "java.util.Date";
        // checking for date format dd-month-yyyy
        else if (input.matches("\\d{2}[-]\\w+[-]\\d{4}")) {
            dataType = "java.util.Date";
        // checking for date format yyyy-mm-dd
        else if (input.matches(
                     "\\d{4}[-]\\d{2}[-]\\d{2}")) {
            dataType = "java.util.Date";
        // checking for String
        else {
            dataType = "java.lang.String";
        System.out.println("The datatype of " + input
                           + " is: " + dataType);




The datatype of 56.73 is: java.lang.Double


Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vivek.singh0302

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.