my dog learned polymorphism
The moose likes Java in General and the fly likes to find the datatype of given string in runtime Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "to find the datatype of given string in runtime" Watch "to find the datatype of given string in runtime" New topic
Author

to find the datatype of given string in runtime

Shobana Sukumar
Greenhorn

Joined: Nov 24, 2005
Posts: 17
im getting data from user at runtime.wat method shalli use to find its datatype?
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

Where and how are you getting the data? In what format is the data? Is it in a file, or a database, or something else? Please provide more details, otherwise it will be hard to give you a good answer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shobana Sukumar
Greenhorn

Joined: Nov 24, 2005
Posts: 17
im getting the data as a string for instance(it can be anything int or date etc). i need to compare the datatype of this to the data type of a column in the database....
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Maybe find the datatype in the database through metadata and then try to convert the user input to that type and see what happens.

You'll get an exception on that Integer if the user entered invalid data.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
I think a common convention in this type of situation is to define what the "data type" is. That is you get each field one at a time in a pre-defined order. Then you can use the various tools available to convert it into the internal representation you wish to use. For example, when you expect an int, you can use Integer.valueOf(); when you expect a date, you can use DateFormat or SimpleDateFormat. Each of these APIs will throw an exception or return a value that indicates an ill-formatted String. You can then use this error condition to give a message to the user and possible ask for the input again.

HTH

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: to find the datatype of given string in runtime
 
Similar Threads
One to Many relation returns duplicate parents
Using Digital Certificates
exceptions and type casting confusion
ntext problem
width of boolean