TopcoderArchiveVisit Topcoder
SRM25 Feb 2003
SRM 136

Datatype

Problem statement, definition, constraints, and public examples.

Problem Statement

When reading data as a string of characters, it is often useful to convert the data into a different datatype, which is easier to work with than a string of characters. For example, many operations are made much easier if the string of characters "123" is converted from a string of characters into a different datatype, such as an int or a float. We want to determine the best datatype to use for some data, given a string of characters. For the purposes of this problem, we will consider converting a string of characters into one of 4 datatypes:

  • "INTEGER" - if the string consists only of the digits '0' - '9'.
  • "BOOLEAN" - if the string is "true" or "false" (ignoring case)
  • "DECIMAL" - if the string contains only digits '0' - '9', and exactly one decimal point ('.'). "123.12", ".123", and "124." are all of type "DECIMAL". Note that by this definition the string "." is classified as a "DECIMAL"
  • "STRING" - if the string is none of the other datatypes.

Your task is to write a class Datatype with a method getType that takes a String, var, and classifies it into one of the above four types, and returns the datatype as a String.

Definition

Class:
Datatype
Method:
getType
Parameters:
String
Returns:
String
Method signature:
String getType(String var)
(be sure your method is public)

Constraints

  • var will contain between 1 and 50 characters, inclusive.
  • Each character in var will be a letter ('a'-'z' or 'A'-'Z'), a digit ('0'-'9'), a decimal point ('.'), a space (' '), or one of the following characters: ,/<>?;':"[]{}\|`~!@#$%^*()_+-=&

Examples

  1. "123"
    Returns: "INTEGER"
  2. "324.1"
    Returns: "DECIMAL"
  3. ".12"
    Returns: "DECIMAL"
  4. "453."
    Returns: "DECIMAL"
  5. "770.555.1212"
    Returns: "STRING"
  6. "TrUe"
    Returns: "BOOLEAN"
  7. "this is just a string"
    Returns: "STRING"
  8. "453 ducks flew 4739.45 miles."
    Returns: "STRING"
  9. "."
    Returns: "DECIMAL"
Back to all problems