Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. public boolean isANumber(String s) {
  2. try {
  3. BigDecimal a = new BigDecimal(s);
  4. return true;
  5. } catch (NumberFormatException e) {
  6. return false;
  7. }
  8. }
  9.  
  10. isNumber[1]
  11.  
  12. public static boolean isNumber(java.lang.String str)
  13. Checks whether the String a valid Java number.
  14.  
  15. Valid numbers include hexadecimal marked with the 0x qualifier, scientific notation and numbers marked with a type qualifier (e.g. 123L).
  16.  
  17. Null and empty String will return false.
  18.  
  19. Parameters:
  20. str - the String to check
  21. Returns:
  22. true if the string is a correctly formatted number
  23.  
  24. public class TestRegexp {
  25. static final String NUM_REGEX=
  26. "-?((([0-9]{1,3})(,[0-9]{3})*)|[0-9]*)(\.[0-9]+)?([Ee][0-9]*)?";
  27. public static boolean isNum(String s) {
  28. return s!=null && s.length()>0 && s.matches(NUM_REGEX);
  29. }
  30. public static void main(String[]args) {
  31. String[] values={
  32. "",
  33. "0",
  34. "0.1",
  35. ".1",
  36. "-.5E5",
  37. "-12,524.5E5",
  38. "-452,456,456,466.5E5",
  39. "-452,456,456,466E5",
  40. "22,22,2.14123415e1",
  41. };
  42. for (String value : values) {
  43. System.out.println(value+" is a number: "
  44. +isNum(value));
  45. }
  46. }
  47.  
  48. }
  49.  
  50. "-?(([0-9]{1,3}(,[0-9{3,3})*)|[0-9]*)(.[0-9]+(e-?[0-9]*)?)?"
  51.  
  52. final String Digits = "(\p{Digit}+)";
  53. final String HexDigits = "(\p{XDigit}+)";
  54. // an exponent is 'e' or 'E' followed by an optionally
  55. // signed decimal integer.
  56. final String Exp = "[eE][+-]?"+Digits;
  57. final String fpRegex =
  58. ("[\x00-\x20]*"+ // Optional leading "whitespace"
  59. "[+-]?(" + // Optional sign character
  60. "NaN|" + // "NaN" string
  61. "Infinity|" + // "Infinity" string
  62.  
  63. // A decimal floating-point string representing a finite positive
  64. // number without a leading sign has at most five basic pieces:
  65. // Digits . Digits ExponentPart FloatTypeSuffix
  66. //
  67. // Since this method allows integer-only strings as input
  68. // in addition to strings of floating-point literals, the
  69. // two sub-patterns below are simplifications of the grammar
  70. // productions from the Java Language Specification, 2nd
  71. // edition, section 3.10.2.
  72.  
  73. // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
  74. "((("+Digits+"(\.)?("+Digits+"?)("+Exp+")?)|"+
  75.  
  76. // . Digits ExponentPart_opt FloatTypeSuffix_opt
  77. "(\.("+Digits+")("+Exp+")?)|"+
  78.  
  79. // Hexadecimal strings
  80. "((" +
  81. // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
  82. "(0[xX]" + HexDigits + "(\.)?)|" +
  83.  
  84. // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
  85. "(0[xX]" + HexDigits + "?(\.)" + HexDigits + ")" +
  86.  
  87. ")[pP][+-]?" + Digits + "))" +
  88. "[fFdD]?))" +
  89. "[\x00-\x20]*"); // Optional trailing "whitespace"
  90.  
  91. if (Pattern.matches(fpRegex, myString))
  92. Double.valueOf(myString); // Will not throw NumberFormatException
  93. else {
  94. // Perform suitable alternative action
  95. }
Add Comment
Please, Sign In to add comment