java.lang.NumberFormatException Java Examples

The following examples show how to use java.lang.NumberFormatException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: constructor.java    From pluotsorbet with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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");

}
 
Example #2
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
/**
 * <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);
}
 
Example #3
Source File: DisplayManagerShellCommand.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: GameController.java    From eb-java-scorekeep with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example #5
Source File: Preferences.java    From ShaderEditor with MIT License 5 votes vote down vote up
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;
}
 
Example #6
Source File: Preferences.java    From ShaderEditor with MIT License 5 votes vote down vote up
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;
}
 
Example #7
Source File: TryCatch.java    From pluotsorbet with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source File: StandardEventsReader.java    From the-one with GNU General Public License v3.0 5 votes vote down vote up
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+"]");
	}
}
 
Example #9
Source File: DatabaseConfiguration.java    From ehcache3-samples with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #10
Source File: GameController.java    From eb-java-scorekeep with Apache License 2.0 5 votes vote down vote up
/** 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);
}
 
Example #11
Source File: Proxy.java    From PADListener with GNU General Public License v2.0 4 votes vote down vote up
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]);
        }
        
    }
}
 
Example #12
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}
 
Example #13
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}
 
Example #14
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}
 
Example #15
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}
 
Example #16
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}
 
Example #17
Source File: NumberUtils.java    From Android-utils with Apache License 2.0 3 votes vote down vote up
/**
 * <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;
    }
}