java.lang.NumberFormatException类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(7.5k)|赞(0)|评价(0)|浏览(119)

本文整理了Java中java.lang.NumberFormatException类的一些代码示例,展示了NumberFormatException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。NumberFormatException类的具体详情如下:
包路径:java.lang.NumberFormatException
类名称:NumberFormatException

NumberFormatException介绍

[英]Thrown when an invalid value is passed to a string-to-number conversion method.
[中]向字符串到数字转换方法传递无效值时引发。

代码示例

代码示例来源:origin: perwendel/spark

/**
 * @param c An ASCII encoded character 0-9 a-f A-F
 * @return The byte value of the character 0-16.
 */
public static int convertHexDigit(char c) {
  int d = ((c & 0x1f) + ((c >> 6) * 0x19) - 0x10);
  if (d < 0 || d > 15) {
    throw new NumberFormatException("!hex " + c);
  }
  return d;
}

代码示例来源:origin: square/okhttp

static int readInt(BufferedSource source) throws IOException {
 try {
  long result = source.readDecimalLong();
  String line = source.readUtf8LineStrict();
  if (result < 0 || result > Integer.MAX_VALUE || !line.isEmpty()) {
   throw new IOException("expected an int but was \"" + result + line + "\"");
  }
  return (int) result;
 } catch (NumberFormatException e) {
  throw new IOException(e.getMessage());
 }
}

代码示例来源:origin: ch.qos.logback/logback-classic

static int parsePortNumber(String portStr) {
  try {
    return Integer.parseInt(portStr);
  } catch (java.lang.NumberFormatException e) {
    e.printStackTrace();
    usage("Could not interpret port number [" + portStr + "].");
    // we won't get here
    return -1;
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public LongColumn appendCell(final String value) {
  try {
    return append(LongColumnType.DEFAULT_PARSER.parseLong(value));
  } catch (final NumberFormatException e) {
    throw new NumberFormatException("Error adding value to column " + name() + ": " + e.getMessage());
  }
}

代码示例来源:origin: google/guava

/**
 * Returns the unsigned {@code int} value represented by the given string.
 *
 * <p>Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:
 *
 * <ul>
 *   <li>{@code 0x}<i>HexDigits</i>
 *   <li>{@code 0X}<i>HexDigits</i>
 *   <li>{@code #}<i>HexDigits</i>
 *   <li>{@code 0}<i>OctalDigits</i>
 * </ul>
 *
 * @throws NumberFormatException if the string does not contain a valid unsigned {@code int} value
 * @since 13.0
 */
@CanIgnoreReturnValue
public static int decode(String stringValue) {
 ParseRequest request = ParseRequest.fromString(stringValue);
 try {
  return parseUnsignedInt(request.rawValue, request.radix);
 } catch (NumberFormatException e) {
  NumberFormatException decodeException =
    new NumberFormatException("Error parsing value: " + stringValue);
  decodeException.initCause(e);
  throw decodeException;
 }
}

代码示例来源:origin: apache/hive

@Override
public String validate(String value) {
 try {
  float fvalue = Float.parseFloat(value);
  if (fvalue < 0 || fvalue > 1) {
   return "Invalid ratio " + value + ", which should be in between 0 to 1";
  }
 } catch (NumberFormatException e) {
  return e.toString();
 }
 return null;
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public FloatColumn appendCell(final String value, AbstractParser<?> parser) {
  try {
    return append(parser.parseFloat(value));
  } catch (final NumberFormatException e) {
    throw new NumberFormatException("Error adding value to column " + name()  + ": " + e.getMessage());
  }
}

代码示例来源:origin: google/guava

/**
 * Returns the unsigned {@code long} value represented by the given string.
 *
 * <p>Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:
 *
 * <ul>
 *   <li>{@code 0x}<i>HexDigits</i>
 *   <li>{@code 0X}<i>HexDigits</i>
 *   <li>{@code #}<i>HexDigits</i>
 *   <li>{@code 0}<i>OctalDigits</i>
 * </ul>
 *
 * @throws NumberFormatException if the string does not contain a valid unsigned {@code long}
 *     value
 * @since 13.0
 */
@CanIgnoreReturnValue
public static long decode(String stringValue) {
 ParseRequest request = ParseRequest.fromString(stringValue);
 try {
  return parseUnsignedLong(request.rawValue, request.radix);
 } catch (NumberFormatException e) {
  NumberFormatException decodeException =
    new NumberFormatException("Error parsing value: " + stringValue);
  decodeException.initCause(e);
  throw decodeException;
 }
}

代码示例来源:origin: perwendel/spark

/**
 * @param c An ASCII encoded character 0-9 a-f A-F
 * @return The byte value of the character 0-16.
 */
public static int convertHexDigit(int c) {
  int d = ((c & 0x1f) + ((c >> 6) * 0x19) - 0x10);
  if (d < 0 || d > 15) {
    throw new NumberFormatException("!hex " + c);
  }
  return d;
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public IntColumn appendCell(final String value) {
  try {
    return append(IntColumnType.DEFAULT_PARSER.parseInt(value));
  } catch (final NumberFormatException e) {
    throw new NumberFormatException("Error adding value to column " + name() + ": " + e.getMessage());
  }
}

代码示例来源:origin: apache/geode

private static long parseLong(String arg) {
 try {
  return Long.parseLong(arg);
 } catch (NumberFormatException ex) {
  throw new IllegalArgumentException(
    "Could not parse -maxOplogSize=" + arg + " because: " + ex.getMessage());
 }
}

代码示例来源:origin: smuyyh/BookReader

private long extractContentLength(Map<String, String> header) {
  long size = 0x7FFFFFFFFFFFFFFFl;
  String contentLength = header.get("content-length");
  if (contentLength != null) {
    try {
      size = Integer.parseInt(contentLength);
    } catch (NumberFormatException ex) {
      ex.printStackTrace();
    }
  }
  return size;
}

代码示例来源:origin: google/guava

private static short parseHextet(String ipPart) {
 // Note: we already verified that this string contains only hex digits.
 int hextet = Integer.parseInt(ipPart, 16);
 if (hextet > 0xffff) {
  throw new NumberFormatException();
 }
 return (short) hextet;
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public IntColumn appendCell(final String value, AbstractParser<?> parser) {
  try {
    return append(parser.parseInt(value));
  } catch (final NumberFormatException e) {
    throw new NumberFormatException("Error adding value to column " + name()  + ": " + e.getMessage());
  }
}

代码示例来源:origin: neo4j/neo4j

private <T extends Number> T assertConvertible( Supplier<T> func )
{
  try
  {
    return func.get();
  }
  catch ( NumberFormatException e )
  {
    throw new InvalidValuesArgumentException( e.getMessage(), e );
  }
}

代码示例来源:origin: daniulive/SmarterStreaming

private void SaveInputPlayBuffer(String bufferText) {
  try {
    Integer intValue;
    intValue = Integer.valueOf(bufferText);
    playBuffer = intValue;
    Log.i(TAG, "Input play buffer:" + playBuffer);
  } catch (NumberFormatException e) {
    Log.i(TAG, "Input play buffer convert exception");
    e.printStackTrace();
  }
}

代码示例来源:origin: prestodb/presto

private static NumberFormatException _badBD(String s) {
    return new NumberFormatException("Value \""+s+"\" can not be represented as BigDecimal");
  }
}

代码示例来源:origin: jtablesaw/tablesaw

@Override
public ShortColumn appendCell(final String value) {
  try {
    return append(ShortColumnType.DEFAULT_PARSER.parseShort(value));
  } catch (final NumberFormatException e) {
    throw new NumberFormatException("Error adding value to column " + name() + ": " + e.getMessage());
  }
}

代码示例来源:origin: alibaba/fescar

private static Integer getClientPortFromChannel(Channel channel) {
  String address = getAddressFromChannel(channel);
  Integer port = 0;
  try {
    if (address.contains(Constants.IP_PORT_SPLIT_CHAR)) {
      port = Integer.parseInt(address.substring(address.lastIndexOf(Constants.IP_PORT_SPLIT_CHAR) + 1));
    }
  } catch (NumberFormatException exx) {
    LOGGER.error(exx.getMessage());
  }
  return port;
}

代码示例来源:origin: libgdx/libgdx

private static int readAPIVersion (File parentFile) {
  File propertiesFile = new File(parentFile, "source.properties");
  Properties properties;
  try {
    properties = readPropertiesFromFile(propertiesFile);
    String versionString = properties.getProperty("AndroidVersion.ApiLevel");
    return Integer.parseInt(versionString);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  } catch (NumberFormatException e) {
    e.printStackTrace();
  }
  return 0;
}

相关文章