![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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));} while (((increase < 0) || (increase > 105)) || (typeof(increase) != typeof(Integer))); Thanks for your time, Anthony Christe Last edited by thechristelegacy; Feb 11th, 2008 at 9:26 PM. Reason: changes && to || |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1
![]() |
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.
|
|
|
|
|
|
#3 |
|
Expert Programmer
|
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. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 258
Rep Power: 2
![]() |
Re: How to compare variable types in Java
the method getClass() will tell you the class.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2008
Location: India
Posts: 58
Rep Power: 1
![]() |
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.
|
|
|
|
|
|
#7 |
|
Java Developer
|
Re: How to compare variable types in Java
you can do something:
increase.getClass().getName()
__________________
[Pushkaraj] Imagination is more important than knowledge – Albert Einstein |
|
|
|
|
|
#8 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#9 |
|
Java Developer
|
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 |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Feb 2008
Posts: 11
Rep Power: 0
![]() |
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/ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |