| Author |
Check to see if a String contains words only?
|
Jamie Cotton
Greenhorn
Joined: Nov 12, 2004
Posts: 16
|
|
Hiya all I wanna check if a variable value is words only or not. So if I had so test should only be allowed to be something like "hello" and not something like "123". If "123" appears I want it to tell me. Such as if i wanna check if the value is an interger I would do Interger.parseInt(String test). If test = 123 then it would be ok but if test = "hello" if would throw an exception. [ April 09, 2005: Message edited by: Jamie Cotton ]
|
 |
Maureen Charlton
Ranch Hand
Joined: Oct 04, 2004
Posts: 218
|
|
Such as if i wanna check if the value is an interger I would do Interger.parseInt(String test). If test = 123 then it would be ok but if test = "hello" if would throw an exception.
Jamie, Try: String test = " "; test = String.parseString(test);
|
 |
Jamie Cotton
Greenhorn
Joined: Nov 12, 2004
Posts: 16
|
|
I tried that but still no luck
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
Originally posted by Jamie Cotton: I tried that but still no luck
Not surprising, considering there is no such method! There's no method in the API which will tell you directly whether a String is a "word" because word-ness is a subjective concept. On the other hand, testing for integer-ness is straightforward. The first thing you need to do is to state your problem clearly. Are you interested in a JTextField which allows you to type only alphabetic characters? That's doable. Are you interested in telling whether a String contains any digits? That's doable, too. But you have to think clearly about exactly what you want to do, and so far, you haven't made a precise statement of what that is.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Jamie Cotton
Greenhorn
Joined: Nov 12, 2004
Posts: 16
|
|
|
Sorry I am interested in a JTextField which allows you to type only alphabetic characters.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
|
OK, that's easy. Use a JFormattedTextField, which lets you specify the format for legal data entries. See Here for a tutorial on using this class, including demo code.
|
 |
miguel lisboa
Ranch Hand
Joined: Feb 08, 2004
Posts: 1281
|
|
|
or you could experiment with regular expressions
|
java amateur
|
 |
 |
|
|
subject: Check to see if a String contains words only?
|
|
|