How to validate the string object for alphabet input

Hi,

I want to check for alphabet (a-z,A-Z), in String object. I need to check the object, whether its contain numerals or special character, in that case, I want to throw an error stating that "value is not valid". It should accept only the a-z or A-Z.

how to do this.

Thanks in advance

Karthi

[324 byte] By [Codecafea] at [2007-10-3 2:59:42]
# 1
Look at the Pattern class and the matches() method.
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:18 > top of Java-index,Java Essentials,Java Programming...
# 2

> I want to check for alphabet (a-z,A-Z), in String

> object. I need to check the object, whether its

> contain numerals or special character, in that case,

> I want to throw an error stating that "value is not

> valid". It should accept only the a-z or A-Z.

> how to do this.

As Rene suggested, you can do this using the Pattern class:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

You can do it also by looping through your String and check with String's charAt(index) method (which returns a char) to see if every char from the String is >= A AND <= z.

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

Good luck.

prometheuzza at 2007-7-14 20:49:18 > top of Java-index,Java Essentials,Java Programming...
# 3

> You can do it also by looping through your String and

> check with String's charAt(index) method

> (which returns a char) to see if every char from the

> String is >= A AND <= z.

There's a gap between the upper-case and the lower-case letters. So a few special chars like underscore will sneak in. You'd need to check for <= Z and >= a too...

CeciNEstPasUnProgrammeura at 2007-7-14 20:49:18 > top of Java-index,Java Essentials,Java Programming...
# 4

> There's a gap between the upper-case and the

> lower-case letters. So a few special chars like

> underscore will sneak in. You'd need to check for <=

> Z and >= a too...

Cripes, I was under the impression there wasn't a gap! Thank for pointing out the error.

@OP: every char should then satisfy the following condition:

char ch = ...

((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

But the Pattern solution would be far shorter.

prometheuzza at 2007-7-14 20:49:18 > top of Java-index,Java Essentials,Java Programming...
# 5
> Cripes, I was under the impression there wasn't a> gap! Thank for pointing out the error.And if you recovered from that shock, go and look up an EBCDIC table. (Not that it's relevant to Java.)
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:19 > top of Java-index,Java Essentials,Java Programming...
# 6
> And if you recovered from that shock, go and look up> an EBCDIC table. (Not that it's relevant to Java.)Jeepers... I'm not touching those IBM mainframes, that's for sure!; )
prometheuzza at 2007-7-14 20:49:19 > top of Java-index,Java Essentials,Java Programming...
# 7
> Jeepers... I'm not touching those IBM mainframes,> that's for sure!> ; )Well, I've only seen a handful of non-IBM mainframes so far, Amdahl mostly, so you might not have much of a choice. ;)
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:19 > top of Java-index,Java Essentials,Java Programming...
# 8
hi, Could u pls, give me some info abt the pattern function with some eg.Thanks for ur response.
Codecafea at 2007-7-14 20:49:19 > top of Java-index,Java Essentials,Java Programming...
# 9
> Could u pls, give me some info abt the pattern> function with some eg.Oh read the darn API, it even comes with an example.
CeciNEstPasUnProgrammeura at 2007-7-14 20:49:19 > top of Java-index,Java Essentials,Java Programming...