如何检查字符串是正数,负数还是数字

时间:2016-01-29 10:30:54

标签: java

问题

我有Student[],我想知道String是否为数字,也可能是负数

测试用例

String

尝试过的东西

我使用String test1 = "abcd"; // Here it must show is not a number String test2 = "abcd-123"; // Here it must show is not a number String test3 = "123"; // Here it must show is a number String test4 = "-.12"; // Here it must show is a number String test5 = "-123"; // Here it must show is a number String test6 = "123.0; // Here it must show is a number String test7 = "-123.00"; // Here it must show is a number String test8 = "-123.15"; // Here it must show is a number String test9 = "09"; // Here it must show is a number String test10 = "0.0"; // Here it must show is a number StringUtils#isNumber,但没有帮助,负数,“09”显示为不是数字

4 个答案:

答案 0 :(得分:5)

你可以试试这个:

try {
    double value = Double.parseDouble(test1);
    if(value<0)
       System.out.println(value + " is negative");
    else
       System.out.println(value + " is possitive");
} catch (NumberFormatException e) {
    System.out.println("String "+ test1 + "is not a number");
}

答案 1 :(得分:2)

根据Double.valueOf的Javadoc:

  

为避免在无效字符串上调用此方法并抛出NumberFormatException,可以使用下面的正则表达式来筛选输入字符串:

  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 section 3.10.2 of
       // The Java™ Language Specification.

       // 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
  }

正则表达是实质性的,但是全面且记录良好。你可以随意修剪它。

答案 2 :(得分:1)

尝试使用catch语句将输入解析为围绕它的整数,如果它没有转换它应该返回输入是一个字符,如果它转换它返回输入是一个数字

答案 3 :(得分:1)

 public static String checknumeric(String str){
        String numericString = null;
        String temp;
      if(str.startsWith("-")){ //checks for negative values
          temp=str.substring(1);
          if(temp.matches("[+]?\\d*(\\.\\d+)?")){
              numericString=str;
          }
      }
        if(str.matches("[+]?\\d*(\\.\\d+)?")) {
            numericString=str;
        }
        return numericString;
    }