Open In App

Finding Data Type of User Input using Regular Expression in Java

Last Updated : 16 Oct, 2020
Summarize
Comments
Improve
Suggest changes
Share
3 Likes
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:

  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.

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:

 
 


Output
The datatype of 56.73 is: java.lang.Double


 


Practice Tags :

Similar Reads