Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 11th, 2008, 9:23 PM   #1
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
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

Last edited by thechristelegacy; Feb 11th, 2008 at 9:26 PM. Reason: changes && to ||
thechristelegacy is offline   Reply With Quote
Old Feb 11th, 2008, 9:47 PM   #2
rhish.franks
Programmer
 
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1 rhish.franks is on a distinguished road
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.
rhish.franks is offline   Reply With Quote
Old Feb 11th, 2008, 10:55 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.

Last edited by titaniumdecoy; Feb 11th, 2008 at 11:06 PM.
titaniumdecoy is offline   Reply With Quote
Old Feb 12th, 2008, 12:59 AM   #4
mrynit
Hobbyist Programmer
 
mrynit's Avatar
 
Join Date: Mar 2006
Location: WA, USA
Posts: 332
Rep Power: 3 mrynit is on a distinguished road
Send a message via AIM to mrynit Send a message via MSN to mrynit Send a message via Yahoo to mrynit Send a message via Skype™ to mrynit
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.
__________________
i dont know much about programming but i try to help
mrynit is offline   Reply With Quote
Old Feb 12th, 2008, 1:09 AM   #5
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 263
Rep Power: 2 Fall Back Son is on a distinguished road
Re: How to compare variable types in Java

the method getClass() will tell you the class.
Fall Back Son is offline   Reply With Quote
Old Feb 12th, 2008, 1:28 AM   #6
rhish.franks
Programmer
 
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1 rhish.franks is on a distinguished road
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.
rhish.franks is offline   Reply With Quote
Old Feb 12th, 2008, 2:33 AM   #7
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 24
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
Re: How to compare variable types in Java

you can do something:

increase.getClass().getName()
__________________
[Pushkaraj]


Imagination is more important than knowledge – Albert Einstein
pushkarajthorat is offline   Reply With Quote
Old Feb 12th, 2008, 2:25 PM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Feb 13th, 2008, 7:36 AM   #9
pushkarajthorat
Java Developer
 
pushkarajthorat's Avatar
 
Join Date: Jun 2006
Location: Solapur, India.
Posts: 24
Rep Power: 0 pushkarajthorat is an unknown quantity at this point
Send a message via Yahoo to pushkarajthorat
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);
__________________
[Pushkaraj]


Imagination is more important than knowledge – Albert Einstein
pushkarajthorat is offline   Reply With Quote
Old Feb 13th, 2008, 9:10 AM   #10
itgalary
Newbie
 
itgalary's Avatar
 
Join Date: Feb 2008
Posts: 11
Rep Power: 0 itgalary is on a distinguished road
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.
__________________
Free Programming help http://www.itgalary.com
Free Revenue Sharing http://blog.itgalary.com
Online Site seeing http://www.itgalary.com/maps/
itgalary is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
First Java Program duale2005 Java 3 May 22nd, 2006 5:17 PM
Java programmers, game developers, artists, be ware! RPG game team is recruiting! atcomputers.us Paid Job Offers 7 Sep 25th, 2005 7:25 PM
variable problem robert_sun C 1 Apr 12th, 2005 2:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:25 AM.

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