Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I know the type of an input value

Status
Not open for further replies.

wangdong

Programmer
Oct 28, 2004
202
CN
The program enables user to enter a value from a command mode, and will return the type of this input value, whether is boolean, integer, double, string or char.

Does anyone know how to get the type of a primitive?

Chinese Java Faq Forum
 
Not allways possible.
A 'true' might be a boolean, might be a String.
A '4' is a valid byte, short, char, String, int, long, float and double.

You could ask the user to provide a suffix like:
4b
4i
4l
etc.
Or you could choose the most restricted value which maches.

seeking a job as java-programmer in Berlin:
 
But all of them have an associated class with parse methods that can tell you if the value can fit one of the primitive types.

Btw, from command line, all values are supposed to be Strings.

Cheers,
Dian
 
I would start by booleans, checking for 'equals ("true")' etc.
Then - check if it's just numbers (optional: leading -).
If yes, and if we ignore byte and short, try to interpret as int.
If too big or small, try as long.

If it contains a dot, interpret it as float (or double).

Else, if it's 1 character long as char.
Else it is a String (which is not a primitive (I prefer: build-in, because they aren't primitive at all - let's only think about division!)).

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top