> Hi,
> I was hoping some one may be able to help me. I am trying to get a piece of
> code to work, which I thought I had right. But as far as I can tell, it does
> not do anything when I run it in jBuilder.
> I want to make sure the string that is collected from Keyboard.getString is
> a upper case letter and the total length of the string is 8 characters long.
A better way to do this would be to use a Regex (regular expression).
String has a method
public boolean matches(String regex);
that would do the job. Use could use it as follows:
aa == Keyboard.getString();
validName = aa.matches("[A-Z]{8}") && (aa.length==8);
This would match a string that has 8 consecutive capitalized characters,
I think. The second half of the boolean expression ensures that the
string only has those 8 characters, since a regex will match any
substring as well.
|\/| /| |2 |<
mehaase(at)sas(dot)upenn(dot)edu
Jim Gibson - 24 May 2005 20:04 GMT
> aa == Keyboard.getString();
> validName = aa.matches("[A-Z]{8}") && (aa.length==8);
[quoted text clipped - 3 lines]
> string only has those 8 characters, since a regex will match any
> substring as well.
You can also anchor the substring to the beginning and end of the line
with '^' and '$', respectively:
validName = aa.matches("^[A-Z]{8}$");