The class Character
has methods to check if a character is letter, number, etc. See all options in documentation, but a solution to your case would be:
for (int i = 0; i < frase.length(); i++) {
char c = frase.charAt(i);
if (Character.isDigit(c)) {
System.out.println(c + " é número");
} else if (Character.isLetter(c)) {
System.out.println(c + " é letra");
} else {
System.out.println(c + " não é letra nem número");
}
}
To another answer only used isDigit
and assumed that if it is not a number, then it is a letter. But if the string has characters that are not letters or numbers (such as space, punctuation marks, emojis, etc.), then the code suggested by it no longer works, as it will erroneously say that all these characters are letters. And since this restriction is not cited, we have to assume that anything can be typed in the text.
We could stop here, but I think it’s worth going a little deeper. The definition of "letter" and "number" goes far beyond our alphabet. For example, the characters below are considered digits:
// todas as linhas abaixo imprimem "true"
System.out.println(Character.isDigit('۸'));
System.out.println(Character.isDigit('۹'));
System.out.println(Character.isDigit('۵'));
This is because isDigit
considers all Unicode-defined digits that are in the category "Number, Decimal Digit" (are all from this list).
The same goes for letters, because isLetter
considers all letters defined by Unicode (are the characters that are in the categories starting with "L" from this list). That is, letters from other alphabets are also considered (for example, Character.isLetter('親')
returns true
).
If the idea is to limit the letters of a
to z
and digits of 0
to 9
(and without considering accented letters and the ç
), then it could be done so:
for (int i = 0; i < frase.length(); i++) {
char c = frase.charAt(i);
if ('0' <= c && c <= '9') {
System.out.println(c + " é número");
} else if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
System.out.println(c + " é letra");
} else {
System.out.println(c + " não é letra nem número");
}
}
If it is to consider accented letters, it would have to have one more condition in the second if
. But you should also take into account the forms of normalization, which I won’t go into detail, but there are much more detailed explanations about here, here and here.
Finally, if the idea is to check only if it is letter or number, without specifying which of these two you want, another option is to use isLetterOrDigit
:
for (int i = 0; i < frase.length(); i++) {
char c = frase.charAt(i);
if (Character.isLetterOrDigit(c)) {
System.out.println(c + " é letra ou número");
} else {
System.out.println(c + " não é letra nem número");
}
}
Remembering that isLetterOrDigit
uses the same criteria as isLetter
and isDigit
(that is, it takes into account all characters defined by Unicode). If you want to do something similar just for our alphabet (without considering accents) and digits of 0
to 9
, would be:
for (int i = 0; i < frase.length(); i++) {
char c = frase.charAt(i);
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) {
System.out.println(c + " é letra ou número");
} else {
System.out.println(c + " não é letra nem número");
}
}
Just for the record, the answer you accepted does not treat cases that have space, comma, punctuation, etc (using the suggested "logic", will say that these characters are letters)
– hkotsubo