0

I have a method that accepts a String argument.

The String could contain an integer, long, double, boolean, or just plain old characters and numbers.

I need an easy way to determine which type the string has.

One way could be to have a bunch of try catch blocks and try to parse it into each types, and if an exception is thrown, then it is probably not that data type. That seems wrong because it's a heuristic, not a deterministic program.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
user965951
  • 167
  • 1
  • 1
  • 7

4 Answers4

2

I think it's generally assumed that you need to know what it is - usually such parameters are either known or come with some meta-data. You could always use regular expressions to look for markers such as digits and full stops.

Useful patterns:

Pattern.compile("(?:\\+|\\-)?\\d+\\.\\d+");` // Matches a double.
Pattern.compile("(?:\\+|\\-)?\\d{numberOfDigitsBeforeYouWantToCallItALong,}"); // Matches longs.
Pattern.compile("(?:\\+|\\-)?\\d{,numberOfDigitsBeforeYouWantToCallItALongMinusOne}"); // Matches ints.
Pattern.compile("true|false|t|f|yes|no|y|n"); // Matches booleans.

Everything else is a String.

Edit: From your edit I see you've added how it's used and you could just use "(?:\\+|\\-)?\\d+" to detect for numbers, and if your target type is either int or long, accept it and parse it as the target type rather than based on the number of digits. Or you could just try to parse directly to the appropriate type and catch the Exception, since you know the expected type anyway.

Vala
  • 5,628
  • 1
  • 29
  • 55
1

If you know what the type should be once you know the parameter, then parsing the string to that type and catching an exception is OK - it's an appropriate use of exceptions to catch invalid data (as long as you're only trying one type...)

MattR
  • 6,908
  • 2
  • 21
  • 30
  • 1
    Agreed. The overhead should be avoided if there's a high likelyhood of throwing and catching an Exception, such as the situation originally described. But this really sounds like it's just a case of "it has to parse to , or there's an error", which is exactly what those Exceptions are for. – Vala Jul 12 '12 at 08:26
0

There are only 3 special cases: true, false, and a number. Just check if the string is "true", then check if it is "false", then use Double.parseDouble() (since a long can fit in a double). If it isn't equal to true or false, and Double.parseDouble throws an exception, then it is just a string

David Watson
  • 2,031
  • 2
  • 13
  • 17
-1

Could you use the Scanner class?

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close(); 

Would output

1
2
Red
Blue

Probably to complicated or what you want though

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 2
    He says the object is an instance of String. I believe he wants to parse the contents. – Vala Jul 12 '12 at 08:16
  • If you read the question carefully you'll notice that he already knows it's a string, he just needs to decide wether it contains a number like "1" or boolean like "true" or its a regular string "ghgh" – Tomer Jul 12 '12 at 08:18