jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Comparing value to it's object/primitive type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Comparing value to it Watch "Comparing value to it New topic
Author

Comparing value to it's object/primitive type

Roy Wira
Greenhorn

Joined: Sep 25, 2002
Posts: 12
Dear All,
I just have one question about comparing a value to it's object/primitive type, hope anyone could help me with this.
Here's the story:
In the project I'm currently in, I need to validate user input :
cboParamType = "java.lang.Integer"
with
txtUserEntry1 = "HelloWorld"
or
txtUserEntry2 = "12345"
When user selected "java.lang.Integer" or other Object types from the ParamType combobox, How to check whether s/he has entered the correct object/primitive type in the User Entry textfield or not?
I am thinking to parse the userentry character by character, but I believe there must be a better way
Thus, in my example :
- comparing paramType with userEntry1 will results false.
- comparing paramType with userEntry2 will results true.
Any help would be appreciated.
regards,
Roy
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Hi,
From the textfield you can take input of string only.
Now you can validate if this String is compatible to premitive type say int like this
function boolean isInteger() {
try {
new Integer(txtField.getText().trim());
return true;
} catch(NumberFormatException e){
e.printStackTrace();
}
return false;
}
if the textfield contains valid integer type, it will now thow any exception so it will return true. Otherwise, it will return false.
Hope this clarifies your doubt.

Sanjeev Kaushik
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Roy Wira:
Dear All,
I just have one question about comparing a value to it's object/primitive type, hope anyone could help me with this.<snip>Roy

Roy, if you're using java 1.4, then a simple test like
myString.matches("\\d+") is all you need.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
[ September 25, 2002: Message edited by: Max Habibi ]

Java Regular Expressions
Roy Wira
Greenhorn

Joined: Sep 25, 2002
Posts: 12
Originally posted by Sanjeev Kaushik:
Hi,
function boolean isInteger() {
try {
new Integer(txtField.getText().trim());
return true;
} catch(NumberFormatException e){
e.printStackTrace();
}
return false;
}

Thanks Sanjeev, It gave an alternative to me, just wondering ... how to get the wrapper (in your example) "Integer" since it depends on user's choice from combo box that returns String casted Object? FYI: I originally put the combo box items as an array of String.
I was figuring out how to use Class.forName() and Object.getClass() but no hint since both value returns String.
Any further help would be greatly appreciated.
For Max ... unfortunately my project is designed using jdk 1.3
Thanks,
Roy
[ September 25, 2002: Message edited by: Roy Wira ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing value to it's object/primitive type
 
Similar Threads
expected:5 but was:5.0000000
Boxing == Long
What is the Initial value of char literal?
NullpointerException
having an string need to take out the integer!