• 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

Comparing value to it's object/primitive type

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Roy Wira
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic