aspose file tools*
The moose likes Beginning Java and the fly likes how can i determine the type of a variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can i determine the type of a variable?" Watch "how can i determine the type of a variable?" New topic
Author

how can i determine the type of a variable?

Adam Brown
Greenhorn

Joined: Jan 08, 2004
Posts: 11
withour examining at the code itself, is there a method that will tell me what type a variable is. For example, i want to distinguish an int from a char.
thanks in advance,
Adam
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I've a feeling there is a better/different way to do what you're trying to do.
In what context are you wanting to know the type of a variable? Is it a variable passed to a method?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by Adam Brown:
withour examining at the code itself, is there a method that will tell me what type a variable is. For example, i want to distinguish an int from a char.
thanks in advance,
Adam

An ugly method is to use the instanceof operator for all primitive types, if you're expecting your variable to be a primitive.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Cheng Wei Lee:
An ugly method is to use the instanceof operator for all primitive types, if you're expecting your variable to be a primitive.

No, you cannot do that. The instanceof operator requires a class or array type as the second operand.
Now, a rather ugly strategy would be to create some overridden methods, one for each primitive datatype to be considered.But again, there is quite likely a better way to handle the situation. We'd need to know more about it to figure out a good solution.
[ April 08, 2004: Message edited by: Dirk Schreckmann ]
Adam Brown
Greenhorn

Joined: Jan 08, 2004
Posts: 11
I apologize for both the delay in this post and the clarification needed. I suppose what I am trying to accomplish is the throwing of an exception in my code if a primitive is of a specific type.
For example, I have a JTextField that reads a double value that will be manipulated in some sort of financial equasion. Granted, I understand that a JTextField takes a string argument and has to be parsed into a double value, but would I have to create a custom Exception class to handle whether or not that value could or could not be converted into a double?
This was a problem that was presented in class, and everyone is kind of chewing on it for the time being (including our professor). It has turned into a kind of competition, actually, to see who can research and find the most elegant solution. If someone does not want to give me the answer but rather point me in the right direction I would be very grateful.
Thanks in advance,
Adam
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
First identify a method that will convert your String into a double.
Then, identify the exceptions that that method throws.


Mike Gershman
SCJP 1.4, SCWCD in process
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I may be missing the obvious, but how can you not know what you have? How can you refer to it and do anything with it unless you have a reference to it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Adam Brown:
would I have to create a custom Exception class to handle whether or not that value could or could not be converted into a double?

Well, no. That's java.lang.NumberFormatException, which is already thrown by every Java method that tries to convert a String into a number.


[Jess in Action][AskingGoodQuestions]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Adam Brown:
I suppose what I am trying to accomplish is the throwing of an exception in my code if a primitive is of a specific type.

What specific type?
For example, I have a JTextField that reads a double value that will be manipulated in some sort of financial equasion. Granted, I understand that a JTextField takes a string argument and has to be parsed into a double value, but would I have to create a custom Exception class to handle whether or not that value could or could not be converted into a double?

I don't see how this has anything to do with determining whether a variable is of a particular primitive type.
If you have an input String, and you want to know if you can turn it into an int, just try to do so and then handle the possible NumberFormatException that could be thrown. Ditto for double.
Now, if you want to figure out if the input value is within the range representable by a char, for example, then you could just first convert it to an int (or long), and then compare the value against Char.MIN_VALUE and Char.MAX_VALUE. You could even go to the extreme and first create a BigInteger or BigDecimal with the input value before determining whether it can be represented by some primitive type.
This was a problem that was presented in class, and everyone is kind of chewing on it for the time being (including our professor).

Perhaps your instructor hasn't explained what (s)he's actually pondering very clearly for your understanding.
If someone does not want to give me the answer but rather point me in the right direction I would be very grateful.

I'm afraid that I don't really understand what you're "chewing on". Your question isn't clear to me.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Adam Brown:
I apologize for both the delay in this post and the clarification needed. I suppose what I am trying to accomplish is the throwing of an exception in my code if a primitive is of a specific type.
For example, I have a JTextField that reads a double value that will be manipulated in some sort of financial equasion. Granted, I understand that a JTextField takes a string argument and has to be parsed into a double value, but would I have to create a custom Exception class to handle whether or not that value could or could not be converted into a double?
This was a problem that was presented in class, and everyone is kind of chewing on it for the time being (including our professor). It has turned into a kind of competition, actually, to see who can research and find the most elegant solution. If someone does not want to give me the answer but rather point me in the right direction I would be very grateful.
Thanks in advance,
Adam

I suspect all you need is to call the appropriate method to convert your String to a double (Hint: check the API docs) and then catch the NumberFormatException that is thrown. I know this is basically what others have said; perhaps wording it differently will help clarify if you still don't understand.
Layne


Java API Documentation
The Java Tutorial
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
I see the problem--whenever you're dealing with user input the affectionately titled Stupid User can put any data they want into the input field. If an exception is the desired result of invalid input, though, Java already has you covered. Just pretend it's a double and parse it using Double.parseDouble(), and if it isn't you'll get the exception you're after--the parseDouble() method will throw it and all you have to do is let it sail on by.
sev
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i determine the type of a variable?