4
How do I find out how many digits a java string has ? For example, user entered with "exemplo123", the string has 3 digits. I’m using this function but it’s not working:
private static int digitos(String text) {
char[] digitos = new char[9];
int digitosTotal = 0;
for (int i = 0; i != 9; i++) {
digitos[i] = (char) i;
}
for (int i = 0; i < digitos.length; i++) {
if (text.indexOf(digitos[i]) != -1) {
digitosTotal++;
}
}
return digitosTotal;
}
You can change
48
for'0'
and57
for'9'
.– Piovezan
@Piovezan legal :D did not know this.
– rray
@Piovezan when you do
if(asciiCode >= '0') ..
the'0'
is automatically converted to 48, would that be?– rray
I don’t know what step Java does this, I think it’s in the lexical analysis. But the character that is between single quotes is converted to its Unicode value (if I’m not mistaken). You can also enter codes u0048 for example but I’m not used to using them.
– Piovezan
https://www.cs.cornell.edu/andru/javaspec/3.doc.html
– Piovezan
@rray, a Java character is a numeric type. So, '0' is considered the character 0, which has value 0x30
– Jefferson Quesado