biblioteca Java para verificar se uma seqüência contém um número * sem exceções *
-
18-09-2019 - |
Pergunta
Eu estou procurando um método que retorna um boolean Se a string é passado é um número válido (por exemplo, "123.55e-9", "-333.556"). I não quiser apenas fazer:
public boolean isANumber(String s) {
try {
BigDecimal a = new BigDecimal(s);
return true;
} catch (NumberFormatException e) {
return false;
}
}
Claramente, a função deve usar uma máquina de estado (DFA) para analisar a cadeia para se certificar de exemplos inválidos não enganá-lo (por exemplo, "-21,22.22.2", "33-2"). Você sabe se existe qualquer biblioteca? Eu realmente não quero escrever-lo eu mesmo, pois é um problema tão óbvio que eu tenho certeza que eu seria re-inventar a roda.
Obrigado,
Nick
Solução
Gostaria de evitar re-inventar esse método e ir com Apache Commons. Se o seu usando Spring, Struts ou muitas outras bibliotecas usados ??Java, muitas vezes eles têm commons Apache incluído. Você vai querer o arquivo commons-lang.jar. Aqui é o método em NumberUtils você gostaria:
isNumber[1]
public static boolean isNumber(java.lang.String str)
Checks whether the String a valid Java number.
Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
Null and empty String will return false.
Parameters:
str - the String to check
Returns:
true if the string is a correctly formatted number
Outras dicas
usar um regexp
A expressão regular exato é especificado no Javadocs para Double.valueOf(String)
.
Para evitar chamar esse método em uma cadeia inválida e ter um
NumberFormatException
ser lançada, a expressão regular a seguir pode ser usado para a tela a cadeia de entrada:final String Digits = "(\\p{Digit}+)"; final String HexDigits = "(\\p{XDigit}+)"; // an exponent is 'e' or 'E' followed by an optionally // signed decimal integer. final String Exp = "[eE][+-]?"+Digits; final String fpRegex = ("[\\x00-\\x20]*"+ // Optional leading "whitespace" "[+-]?(" + // Optional sign character "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string // A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2. // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+ // . Digits ExponentPart_opt FloatTypeSuffix_opt "(\\.("+Digits+")("+Exp+")?)|"+ // Hexadecimal strings "((" + // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "(\\.)?)|" + // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"); // Optional trailing "whitespace" if (Pattern.matches(fpRegex, myString)) Double.valueOf(myString); // Will not throw NumberFormatException else { // Perform suitable alternative action }
Aqui está uma função de utilidade regexp baseado funcionando bem (não poderia caber "" o check-in a regexp, mantendo-lo legível):
public class TestRegexp {
static final String NUM_REGEX=
"-?((([0-9]{1,3})(,[0-9]{3})*)|[0-9]*)(\\.[0-9]+)?([Ee][0-9]*)?";
public static boolean isNum(String s) {
return s!=null && s.length()>0 && s.matches(NUM_REGEX);
}
public static void main(String[]args) {
String[] values={
"",
"0",
"0.1",
".1",
"-.5E5",
"-12,524.5E5",
"-452,456,456,466.5E5",
"-452,456,456,466E5",
"22,22,2.14123415e1",
};
for (String value : values) {
System.out.println(value+" is a number: "
+isNum(value));
}
}
}
É uma expressão regular deve fazer o truque. Eu só sei .Net regexp, mas todas as línguas regex são bastante semelhantes de modo que este deve começar. Eu não testá-lo assim que você pode querer chutá-la em torno de um pouco com a classe regex Java.
"-?(([0-9]{1,3}(,[0-9{3,3})*)|[0-9]*)(\.[0-9]+(e-?[0-9]*)?)?"
Alguns dos sintaxe de controle Regex:
? - Opcional elemento
| - OR operador. Basicamente eu permiti números com ou sem vírgulas se eles foram formatados corretamente.
[] - Conjunto de caracteres permitidos
{,} - máxima mínima de elemento
* - Qualquer número de elementos, 0 ao infinito
+ - Pelo menos um elemento, 1 ao infinito
\ - Escape caráter
. - Qualquer personagem (Daí por que ele tinha escapado)