Finding Data Type of User Input using Regular Expression in Java Last Updated : 16 Oct, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Integer: Numeric datatypes like byte, short, int, long take the form of an Integer object.Double: Decimal datatypes like float and double take the form of Double object.Date: Date in any format (like dd-mm-yyyy or dd/mm/yyyy) is a part of java.util.DateString: All remaining inputs come under the String type. Note: Character inputs and boolean values will also be considered as string. Examples: 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 Approach: 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: Java 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); } } OutputThe datatype of 56.73 is: java.lang.Double Comment More infoAdvertise with us Next Article How to validate identifier using Regular Expression in Java S SAKSHIKULSHRESHTHA Follow Improve Article Tags : Java Java Programs java-regular-expression Practice Tags : Java Similar Reads How to validate identifier using Regular Expression in Java Given a string str, the task is to check whether the string is a valid identifier or not using the Regular Expression. The valid rules for defining Java identifiers are: It must start with either lower case alphabet[a-z] or upper case alphabet[A-Z] or underscore(_) or a dollar sign($).It should be a 2 min read How to validate a Password using Regular Expressions in Java Given a password, the task is to validate the password with the help of Regular Expression. A password is considered valid if all the following constraints are satisfied: It contains at least 8 characters and at most 20 characters.It contains at least one digit.It contains at least one upper case al 3 min read Java Program to Convert String to String Array Using Regular Expression Regular Expressions or Regex (in short) is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are few areas of strings where Regex is widely used to define the constraints. Regular Expressions are provided un 3 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read Count Occurrences of a Given Character using Regex in Java Given a string and a character, the task is to make a function that counts the occurrence of the given character in the string using Regex. Examples: Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in 2 min read How to Check if a String Contains only Digits in Java? In Java, to check if a string contains only digits, we can use various methods such as simple iteration, regular expressions, streams, etc.Example:The example below uses the Character.isDigit() method to check each character of the string to ensure all characters are digits. This is the most simple 3 min read Like