Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   How to compare variable types in Java (http://www.programmingforums.org/showthread.php?t=15164)

thechristelegacy Feb 11th, 2008 10:23 PM

How to compare variable types in Java
 
How do I compare types in Java?

I'm doing input checking for the following code

:

          do {
                        System.out.print("By how many px's would you like to increase(0 to 105):");
                        increase = read.nextInt();
                } while ((increase < 0) || (increase > 105));

Currently it only checks if the input is between 0 and 105. Increase is declared as an Int and I want to add something to the while so that it would work something like
:

} while (((increase < 0) || (increase > 105)) || (typeof(increase) != typeof(Integer)));
Does this capability exists in Java? If not, is there an alternative method to complete the task?

Thanks for your time,
Anthony Christe

rhish.franks Feb 11th, 2008 10:47 PM

Re: How to compare variable types in Java
 
Can you elaborate yourself a bit more? But whatever you have given right now it can be solved by exception handeling. I am not a master of exception handeling but if you send those two inputs as arguements to a method, you can use IllegalArgumentException.

titaniumdecoy Feb 11th, 2008 11:55 PM

Re: How to compare variable types in Java
 
Assuming the increase variable in the code you posted is a Scanner, you can either use the hasNextInt() method to determine if the data waiting to be read can be interpreted as an int value, or catch an exception.

mrynit Feb 12th, 2008 1:59 AM

Re: How to compare variable types in Java
 
you mean making sure you dont add a float to an int? java will auto cast so it works as a float.

Fall Back Son Feb 12th, 2008 2:09 AM

Re: How to compare variable types in Java
 
the method getClass() will tell you the class.

rhish.franks Feb 12th, 2008 2:28 AM

Re: How to compare variable types in Java
 
but the main requirement is that to tell explicitely to user that the data entered by user for two different variables is not of same type. Implicite conversion will take place, but he wants that if types are not same the program should not go to evaluation sothere should not be any type casting.

pushkarajthorat Feb 12th, 2008 3:33 AM

Re: How to compare variable types in Java
 
you can do something:

increase.getClass().getName()

titaniumdecoy Feb 12th, 2008 3:25 PM

Re: How to compare variable types in Java
 
There is absolutely no reason to use getClass(). Scanner.nextInt() will either return an int or throw an exception. If you are reading that value into another type such as a float, that variable will be a float regardless of what the user enters.

pushkarajthorat Feb 13th, 2008 8:36 AM

Re: How to compare variable types in Java
 
You mis-located the change..



:

} while (((increase < 0) || (increase > 105)) || increase.getClass().getName().equals("java.lang.Integer);

itgalary Feb 13th, 2008 10:10 AM

Re: How to compare variable types in Java
 
Yeah i first thought instanceof can be used.

instanceof i think can be used for objects only.


All times are GMT -5. The time now is 3:52 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC