Let's take the UI out of the equation for moment.
To test if a String
is empty, you have a number of options, based on what you consider an empty String
to be, for example...
if (text == null || text.trim().isEmpty()) {...}
Will catch occurrences where the String
value is null
or is of 0
length when the leading and trailing spaces are removed.
To check if a String
contains a valid set of characters, it would be much simpler to use the regular expression functionality supplied by the API, for example...
if (text.matches("[a-z\\-_]+")) {
System.out.println("Matches");
}
Will match all characters between a
and z
and the _
, -
characters. This means that the String
must be in lower case ;)