File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can i determine the type of a variable?

 
Adam Brown
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First identify a method that will convert your String into a double.
Then, identify the exceptions that that method throws.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sever oon
Ranch Hand
Posts: 268
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic