• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to determine if argument passed is int?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to determine if the argument passed to a method is of type int? If you don't have control what will be passed (such as from user input), and the method specifies an int parameter, how to control this? (Other than try/catch).
Would appreciate any input, and any code examples. Thanks.
cmi
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method requires an int, it will do a widening conversion from byte, short or char. User input is usually of type String.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christina Ishii:
Is there a way to determine if the argument passed to a method is of type int? If you don't have control what will be passed (such as from user input), and the method specifies an int parameter, how to control this? (Other than try/catch).
Would appreciate any input, and any code examples. Thanks.
cmi


Im kinda new at this but you may try casting
Example:
float source;
int dest = (int) source;
or if the input is a string
Example :
String source = "25";
int dest = Integer.parseInt(source);
Well anyways hope this helps
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's assume the need is to differentiate among inputs of the following types : "123" "123.45" " 'leading blanks'bc432" "'leading blanks'dgw56.93" as examples.
That is, strings containing int, float/double, an intended int but with erroneously entered spaces and chars, and the same for a double.
You need to
1> make sure the input string has a length > 0
2> for the length of the string, examine each char in turn. If its not what you're looking for [ in this case belonging to an int ] discard it.
3> when done, you have a legitimate string of type "12345" which you can now convert; int i = Integer.parseInt( "12345" );
If you try Integer.parseInt with a string having leading blanks you'll get a run-time error.

[This message has been edited by Don Smathers (edited March 24, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the method signature uses an int and you pass it anything that is not an int or can not be widened into an int, the method will just not get called.
The line calling it will not compile with a "no such constructor" message.
Anything else WILL be an int when it hits the inside of your method.
Now if your method takes, say an String as a parameter (common situation from args[] in main), and you really want it to be only an int, then you really SHOULD use a try catch block to handle the bad situation.
<pre>
void myMethod(String s)
{
int i=0;
try
{
i = Integer.parseInt(s);
}
catch
{
System.out.println("Silly user: only use integers!");
System.exit(0);
}
}
</pre>

[This message has been edited by Cindy Glass (edited March 24, 2001).]
 
Christina Ishii
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your ideas.

Originally posted by Cindy Glass:
If the method signature uses an int and you pass it anything that is not an int or can not be widened into an int, the method will just not get called.
The line calling it will not compile with a "no such constructor" message.


I am coding with other programmers, and have been instructed to be ready for *any* possibility. I don't know how exactly they are going to call my method, or what argument they are going to pass. So I have to think of all angles, and make sure the program continues, with the proper messages. The try/catch seems obvious, but I want to make sure I am not missing something - any more "common" way to accomplish this idea. Thanks again for your comments.
cmi

[This message has been edited by Christina Ishii (edited March 24, 2001).]
[This message has been edited by Christina Ishii (edited March 24, 2001).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
with objects you can use instanceof but int is not an object.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have been instructed to be ready for *any* possibility
It would be IMPOSSIBLE to cover every combination of multiple parameter signatures. However, if you know that they will only pass in ONE parameter, then a common way of handling this is with constructor overloading. You make a constructor for any typical thing that might be passed in as a parameter (making sure that you have all the primitives covered) and handle them accordingly, and then you have the last ditch constructor which takes in an Object parameter.
Of course you get to be clever enough to know how to handle all of this variety of input and get meaningfull results .
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randall Twede:
with objects you can use instanceof but int is not an object.


I just read something about class wrappers for integers in java.lang.Integer which allows them to be treated as objects. Maybe there is a helper function in there that can handle testing for an instance of an integer without too much trouble.
 
reply
    Bookmark Topic Watch Topic
  • New Topic