类java.lang.NumberFormatException源码实例Demo

下面列出了怎么用java.lang.NumberFormatException的API类实例代码及写法,或者点击链接到github查看源代码。

private int setBrightness() {
    String brightnessText = getNextArg();
    if (brightnessText == null) {
        getErrPrintWriter().println("Error: no brightness specified");
        return 1;
    }
    float brightness = -1;
    try {
        brightness = Float.parseFloat(brightnessText);
    } catch (NumberFormatException e) {
    }
    if (brightness < 0 || brightness > 1) {
        getErrPrintWriter().println("Error: brightness should be a number between 0 and 1");
        return 1;
    }
    mService.setBrightness((int) brightness * 255);
    return 0;
}
 
源代码2 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to a <code>BigDecimal</code>.</p>
 *
 * <p>Returns <code>null</code> if the string is <code>null</code>.</p>
 *
 * @param str  a <code>String</code> to convert, may be null
 * @return converted <code>BigDecimal</code> (or null if the input is null)
 * @throws NumberFormatException if the value cannot be converted
 */
public static BigDecimal createBigDecimal(final String str) {
    if (str == null) {
        return null;
    }
    // handle JDK1.3.1 bug where "" throws IndexOutOfBoundsException
    if (StringUtils.isSpace(str)) {
        throw new NumberFormatException("A blank string is not a valid number");
    }
    if (str.trim().startsWith("--")) {
        // this is protection for poorness in java.lang.BigDecimal.
        // it accepts this as a legal value, but it does not appear
        // to be in specification of class. OS X Java parses it to
        // a wrong value.
        throw new NumberFormatException(str + " is not a valid number.");
    }
    return new BigDecimal(str);
}
 
源代码3 项目: pluotsorbet   文件: constructor.java
/**
 * Runs the test using the specified harness.
 *
 * @param harness  the test harness (<code>null</code> not permitted).
 */
public void test(TestHarness harness)
{
    NumberFormatException object1 = new NumberFormatException();
    harness.check(object1 != null);
    harness.check(object1.toString(), "java.lang.NumberFormatException");

    NumberFormatException object2 = new NumberFormatException("nothing happens");
    harness.check(object2 != null);
    harness.check(object2.toString(), "java.lang.NumberFormatException: nothing happens");

    NumberFormatException object3 = new NumberFormatException(null);
    harness.check(object3 != null);
    harness.check(object3.toString(), "java.lang.NumberFormatException");

}
 
源代码4 项目: eb-java-scorekeep   文件: GameController.java
/** PUT /game/SESSION/GAME/starttime/STARTTIME **/
@RequestMapping(value="/{gameId}/starttime/{startTime}",method=RequestMethod.PUT)
public void setStartTime(@PathVariable String sessionId, @PathVariable String gameId, @PathVariable String startTime) throws SessionNotFoundException, GameNotFoundException, NumberFormatException {
  Game game = gameFactory.getGame(sessionId, gameId);
  Long seconds = Long.parseLong(startTime);
  Date date = new Date(seconds);
  logger.info("Setting start time.");
  game.setStartTime(date);
  logger.info("Start time: " + game.getStartTime());
  model.saveGame(game);
}
 
源代码5 项目: eb-java-scorekeep   文件: GameController.java
/** PUT /game/SESSION/GAME/endtime/ENDTIME **/
@RequestMapping(value="/{gameId}/endtime/{endTime}",method=RequestMethod.PUT)
public void setEndTime(@PathVariable String sessionId, @PathVariable String gameId, @PathVariable String endTime) throws SessionNotFoundException, GameNotFoundException, NumberFormatException {
  Game game = gameFactory.getGame(sessionId, gameId);
  Long seconds = Long.parseLong(endTime);
  Date date = new Date(seconds);
  game.setEndTime(date);
  model.saveGame(game);
}
 
源代码6 项目: ehcache3-samples   文件: DatabaseConfiguration.java
private String getValidPortForH2() throws NumberFormatException {
    int port = Integer.parseInt(env.getProperty("server.port"));
    if (port < 10000) {
        port = 10000 + port;
    } else {
        if (port < 63536) {
            port = port + 2000;
        } else {
            port = port - 2000;
        }
    }
    return String.valueOf(port);
}
 
源代码7 项目: the-one   文件: StandardEventsReader.java
private int convertToInteger(String str){
	String dataUnit = str.replaceAll("[\\d.]","").trim();
	String numericPart = str.replaceAll("[^\\d.]","");
	int number = Integer.parseInt(numericPart);

	if (dataUnit.equals("k")) {
		return (number * 1000);
	}
	else if (dataUnit.equals("M")) {
		return (number * 1000000);
	}
	else if (dataUnit.equals("G")) {
		return (number * 1000000000);
	}
	else if (dataUnit.equals("kiB")) {
		return (number * 1024);
	}
	else if (dataUnit.equals("MiB")) {
		return (number * 1048576);
	}
	else if (dataUnit.equals("GiB")) {
		return (number * 1073741824);
	}
	else{
		throw new NumberFormatException("Invalid number format for StandardEventsReader: ["+str+"]");
	}
}
 
源代码8 项目: pluotsorbet   文件: TryCatch.java
/**
 * Runs the test using the specified harness.
 *
 * @param harness  the test harness (<code>null</code> not permitted).
 */
public void test(TestHarness harness)
{
    // flag that is set when exception is caught
    boolean caught = false;
    try {
        throw new NumberFormatException("NumberFormatException");
    }
    catch (NumberFormatException e) {
        // correct exception was caught
        caught = true;
    }
    harness.check(caught);
}
 
源代码9 项目: ShaderEditor   文件: Preferences.java
private static int parseInt(String s, int preset) {
	try {
		if (s != null && s.length() > 0) {
			return Integer.parseInt(s);
		}
	} catch (NumberFormatException e) {
		// use preset
	}

	return preset;
}
 
源代码10 项目: ShaderEditor   文件: Preferences.java
private static long parseLong(String s, long preset) {
	try {
		if (s != null && s.length() > 0) {
			return Long.parseLong(s);
		}
	} catch (NumberFormatException e) {
		// use preset
	}

	return preset;
}
 
源代码11 项目: PADListener   文件: Proxy.java
private void parseListenerConfig() {
    List<String> listListeners = _framework.getListeners();
    
    String[] listeners = new String[listListeners.size()];
    listListeners.toArray(listeners);

    String addr = "";
    String portAsString = null;
    int port = 0;
    HttpUrl base;
    boolean primary = false;

    for (int i = 0; i < listeners.length; i++) {
        addr = "";
        portAsString = null;
        String[] addrParts = listeners[i].split(":");
        for (int j = 0; j < addrParts.length; j++) {
            if (j < addrParts.length - 1){
                addr += addrParts[j];
                if (j < addrParts.length - 2){
                    addr += ":";
                }
            }else{
                portAsString = addrParts[j];
            }
        }
        try {
            port = Integer.parseInt(portAsString.trim());
        } catch (NumberFormatException nfe) {
            System.err.println("Error parsing port for " + listeners[i]
                    + ", skipping it!");
            continue;
        }
        /*
        prop = "Proxy.listener." + listeners[i] + ".base";
        value = Preferences.getPreference(prop, "");
        if (value.equals("")) {
            base = null;
        } else {
            try {
                base = new HttpUrl(value);
            } catch (MalformedURLException mue) {
                _logger.severe("Malformed 'base' parameter for listener '"
                        + listeners[i] + "'");
                break;
            }
        }

        prop = "Proxy.listener." + listeners[i] + ".primary";
        value = Preferences.getPreference(prop, "false");
        primary = value.equalsIgnoreCase("true")
                || value.equalsIgnoreCase("yes");
        */
        base = null;
        if (!addr.equalsIgnoreCase("") && port != 0){
            _listeners.put(new ListenerSpec(addr, port, base, primary, false, false, _captureData, _useFakeCerts, _storeSslAsPcap), null);
            if (Preferences.getPreferenceBoolean("preference_proxy_transparent", false)){
                _listeners.put(new ListenerSpec(addr, Constants.TRANSPARENT_PROXY_HTTP, base, primary, true, false, _captureData, _useFakeCerts, _storeSslAsPcap), null);
                _listeners.put(new ListenerSpec(addr, Constants.TRANSPARENT_PROXY_HTTPS, base, primary, true, true, _captureData, _useFakeCerts, _storeSslAsPcap), null);
            }
        }else{
            _logger.fine("Warrning Skipping " + listeners[i]);
        }
        
    }
}
 
源代码12 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to an <code>int</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string is <code>null</code>, the default value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toInt(null, 1) = 1
 *   NumberUtils.toInt("", 1)   = 1
 *   NumberUtils.toInt("1", 0)  = 1
 * </pre>
 *
 * @param str  the string to convert, may be null
 * @param defaultValue  the default value
 * @return the int represented by the string, or the default if conversion fails
 * @since 2.1
 */
public static int toInt(final String str, final int defaultValue) {
    if(str == null) {
        return defaultValue;
    }
    try {
        return Integer.parseInt(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
源代码13 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to a <code>long</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string is <code>null</code>, the default value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toLong(null, 1L) = 1L
 *   NumberUtils.toLong("", 1L)   = 1L
 *   NumberUtils.toLong("1", 0L)  = 1L
 * </pre>
 *
 * @param str  the string to convert, may be null
 * @param defaultValue  the default value
 * @return the long represented by the string, or the default if conversion fails
 * @since 2.1
 */
public static long toLong(final String str, final long defaultValue) {
    if (str == null) {
        return defaultValue;
    }
    try {
        return Long.parseLong(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
源代码14 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to a <code>float</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string <code>str</code> is <code>null</code>, the default
 * value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toFloat(null, 1.1f)   = 1.0f
 *   NumberUtils.toFloat("", 1.1f)     = 1.1f
 *   NumberUtils.toFloat("1.5", 0.0f)  = 1.5f
 * </pre>
 *
 * @param str the string to convert, may be <code>null</code>
 * @param defaultValue the default value
 * @return the float represented by the string, or defaultValue
 *  if conversion fails
 * @since 2.1
 */
public static float toFloat(final String str, final float defaultValue) {
    if (str == null) {
        return defaultValue;
    }
    try {
        return Float.parseFloat(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
源代码15 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to a <code>double</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string <code>str</code> is <code>null</code>, the default
 * value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toDouble(null, 1.1d)   = 1.1d
 *   NumberUtils.toDouble("", 1.1d)     = 1.1d
 *   NumberUtils.toDouble("1.5", 0.0d)  = 1.5d
 * </pre>
 *
 * @param str the string to convert, may be <code>null</code>
 * @param defaultValue the default value
 * @return the double represented by the string, or defaultValue
 *  if conversion fails
 * @since 2.1
 */
public static double toDouble(final String str, final double defaultValue) {
    if (str == null) {
        return defaultValue;
    }
    try {
        return Double.parseDouble(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
源代码16 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to a <code>byte</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string is <code>null</code>, the default value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toByte(null, 1) = 1
 *   NumberUtils.toByte("", 1)   = 1
 *   NumberUtils.toByte("1", 0)  = 1
 * </pre>
 *
 * @param str  the string to convert, may be null
 * @param defaultValue  the default value
 * @return the byte represented by the string, or the default if conversion fails
 * @since 2.5
 */
public static byte toByte(final String str, final byte defaultValue) {
    if(str == null) {
        return defaultValue;
    }
    try {
        return Byte.parseByte(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
源代码17 项目: Android-utils   文件: NumberUtils.java
/**
 * <p>Convert a <code>String</code> to an <code>short</code>, returning a
 * default value if the conversion fails.</p>
 *
 * <p>If the string is <code>null</code>, the default value is returned.</p>
 *
 * <pre>
 *   NumberUtils.toShort(null, 1) = 1
 *   NumberUtils.toShort("", 1)   = 1
 *   NumberUtils.toShort("1", 0)  = 1
 * </pre>
 *
 * @param str  the string to convert, may be null
 * @param defaultValue  the default value
 * @return the short represented by the string, or the default if conversion fails
 * @since 2.5
 */
public static short toShort(final String str, final short defaultValue) {
    if(str == null) {
        return defaultValue;
    }
    try {
        return Short.parseShort(str);
    } catch (final NumberFormatException nfe) {
        return defaultValue;
    }
}
 
 类所在包
 类方法
 同包方法