This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Check to see if a String contains words only? 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 "Check to see if a String contains words only?" Watch "Check to see if a String contains words only?" New topic
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: 24183
    
  34

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: 24183
    
  34

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Check to see if a String contains words only?
 
Similar Threads
check duplicate key and value in hashmap
Regular expression to search for words not beginning with a single quote and with or without spaces
Formatting numbers with patterns
Doubt About Strings
Collections of Objects within an ActionForm