2

I need a way to return the type of an object (as a string, or class of the object, whichever is easier) given a string with the value in it. I could parse it with each of the types I want, but there are problems associated with that (such as an int may also be parsed as a long, a float can be parsed as a double). Here's my sketch:

private String typeOf(String test) {
    if (test == "true" || test == "false")
        return "boolean";
    else if (// its a number)
        // figure out a way to parse all number types?
        // return corresponding type
    else
        // can't be parsed with any boolean or number types... just a string
        return "string";
}

The types I am checking are: String, long, int, short, byte, byte[], float, and double. Remember I need the SPECIFIC type in order to accomplish the make-or-break functionality of this program. Any help is appreciated.

I'm trying to make a Named Binary Tag (specifications here) that holds a specific type of data given by user input (a JOptionPane - input dialog). In order to make the correct type of tag, I need to know the type of the data given. (Is there a way to get the input from the user where I can avoid the String problem entirely?) Having a String to work with in the first place has become the main source of my issue.

lucis-fluxum
  • 64
  • 1
  • 8

3 Answers3

3

To start your String checking is incorrect. Aside from that think about the different datatypes and their possible values.

An int or Integer's possibles values all lie within the ranges of a long. The same is true for Floats and Doubles. You will need to figure out which Datatype's ranges are subsets of which and test in that order. As an example here is a quick snippit for testing for Integers and Longs.

private String typeOf(String suppliedTest) {
    String test = suppliedTest.strip().toLower();
    if (test.equals("true") || test.equals("false"))
        return "boolean";
    try {
        int testInt = Integer.parseInt(test);
        return "integer";
    } catch (NumberFormatException e) {}
    try {
        long testLong = Long.parseLong(test);
        return "long";
    catch (NumberFormatException e) {}
    ...
}
indivisible
  • 4,892
  • 4
  • 31
  • 50
0

You can use class specific parsing methods to check, if a String represents a certain type, such as

  • Double.parseDouble()
  • Integer.parseInt()

these methods will throw NumberFormatException if the input string does not contain a value, that represents the appropriate type.

Warlord
  • 2,798
  • 16
  • 21
  • you need to decide the priority of types and order checks in such way, that (for example) you check for double only if it's not an integer. – Warlord Mar 01 '14 at 20:00
0

You don't have enough different to know the difference. All you can do is find the smallest type when can represent that data. a 0 could be a String, long, int, short, byte, float or double. You will need to decide which would be the most appropriate response.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130