Example usage for java.lang NumberFormatException NumberFormatException

List of usage examples for java.lang NumberFormatException NumberFormatException

Introduction

In this page you can find the example usage for java.lang NumberFormatException NumberFormatException.

Prototype

public NumberFormatException(String s) 

Source Link

Document

Constructs a NumberFormatException with the specified detail message.

Usage

From source file:org.okinawaopenlabs.ofpm.utils.OFPMUtils.java

/**
 * transfer macAddresslong// w  w w .j  av a  2 s  .  c  om
 * @param mac String ex.AA:BB:CC:DD:EE:FF
 * @return long transfered
 * @throws NullPointerException mac is null
 * @throws NumberFormatException if the String does not contain a parsable long
 */
public static long macAddressToLong(String mac) throws NullPointerException, NumberFormatException {
    //String macTmp = StringUtils.join(mac.split(":"));
    String hexMac = mac.replace(":", "");
    long longMac = Long.decode("0x" + hexMac);
    if (longMac < MIN_MACADDRESS_VALUE || MAX_MACADDRESS_VALUE < longMac) {
        String errMsg = String.format(PARSE_ERROR, mac);
        throw new NumberFormatException(errMsg);
    }
    return longMac;
}

From source file:Main.java

/**
 * Parses a time period into a long./*w  w  w .ja va 2 s.com*/
 *
 * Translates possible [msec|sec|min|h] suffixes
 *
 * For example:
 *   "1"      ->  1 (msec)
 *   "1msec   ->  1 (msec)
 *   "1sec"   ->  1000 (msecs)
 *   "1min"   ->  60000 (msecs)
 *   "1h"     ->  3600000 (msecs)
 * 
 * Accepts negative periods, e.g. "-1"
 * 
 * @param period the stringfied time period
 * @return the parsed time period as long
 * @throws NumberFormatException
 */
public static long parseTimePeriod(String period) {
    try {
        String s = period.toLowerCase();
        long factor;

        // look for suffix
        if (s.endsWith("msec")) {
            s = s.substring(0, s.lastIndexOf("msec"));
            factor = MSEC;
        } else if (s.endsWith("sec")) {
            s = s.substring(0, s.lastIndexOf("sec"));
            factor = SECS;
        } else if (s.endsWith("min")) {
            s = s.substring(0, s.lastIndexOf("min"));
            factor = MINS;
        } else if (s.endsWith("h")) {
            s = s.substring(0, s.lastIndexOf("h"));
            factor = HOUR;
        } else {
            factor = 1;
        }
        return Long.parseLong(s) * factor;
    } catch (RuntimeException e) {
        // thrown in addition when period is 'null'
        throw new NumberFormatException("For input time period: '" + period + "'");
    }
}

From source file:Main.java

/**
 * Converts a character array to an integer of base radix.
 * <br><br>/*  ww w.  j  ava2s  .c  o  m*/
 * Array constraints are:
 * <li>Number must be less than 10 digits</li>
 * <li>Number must be positive</li>
 * @param cArray Character Array representation of number
 * @param radix Number base to use
 * @return integer value of number
 * @throws NumberFormatException
 */
public static int parseInt(char[] cArray, int radix) throws NumberFormatException {
    int length = cArray.length;
    if (length > 9)
        throw new NumberFormatException("Number can have maximum 9 digits");
    int result = 0;
    int index = 0;
    int digit = Character.digit(cArray[index++], radix);
    if (digit == -1)
        throw new NumberFormatException("Char array contains non-digit");
    result = digit;
    while (index < length) {
        result *= radix;
        digit = Character.digit(cArray[index++], radix);
        if (digit == -1)
            throw new NumberFormatException("Char array contains non-digit");
        result += digit;
    }
    return result;
}

From source file:com.doitnext.http.router.responsehandlers.DefaultErrorHandlerTest.java

@Test
public void testHandleResponseYes() throws IOException {
    DefaultErrorHandler h = new DefaultErrorHandler();
    Exception responseData = new IllegalArgumentException("Hidey Ho!!", new NumberFormatException("3r3"));
    MockHttpServletResponse response = new MockHttpServletResponse();
    boolean handled = h.handleResponse(null, null, response, responseData);
    Assert.assertTrue(handled);/* ww w.  j a v  a 2s  . c  om*/

    String content = response.getContentAsString();
    String expectedContent = IOUtils.toString(this.getClass().getResourceAsStream("error1.dat"), "UTF-8");
    Assert.assertEquals(expectedContent, content);
}

From source file:org.jasypt.spring31.xml.encryption.AbstractEncryptionBeanDefinitionParser.java

protected final void processIntegerAttribute(final Element element, final BeanDefinitionBuilder builder,
        final String attributeName, final String propertyName) {
    final String attributeValue = element.getAttribute(attributeName);
    if (StringUtils.hasText(attributeValue)) {
        try {/*from  www  .  j  a  v  a 2  s  .  com*/
            final Integer attributeIntegerValue = Integer.valueOf(attributeValue);
            builder.addPropertyValue(propertyName, attributeIntegerValue);
        } catch (final NumberFormatException e) {
            throw new NumberFormatException(
                    "Config attribute \"" + attributeName + "\" is not a valid integer");
        }
    }
}

From source file:Main.java

/**
 * Same like parseTimePeriod(), but guards for negative entries.
 * //  w  w  w.  ja  v a2 s  .  co m
 * @param period the stringfied time period
 * @return the parsed time period as long
 * @throws NumberFormatException
 */
public static long parsePositiveTimePeriod(String period) {
    long retval = parseTimePeriod(period);
    if (retval < 0) {
        throw new NumberFormatException("Negative input time period: '" + period + "'");
    }
    return retval;
}

From source file:com.github.nlloyd.hornofmongo.adaptor.NumberLong.java

@JSConstructor
public NumberLong(final Object obj) {
    super();// w  ww.  ja va 2s .  c om
    if (obj instanceof Number) {
        realLong = ((Number) obj).longValue();
    } else if (!(obj instanceof Undefined)) {
        String str = Context.toString(obj);
        Number num = NumberUtils.createNumber(str);
        if ((num instanceof BigInteger) || (num instanceof BigDecimal))
            throw new NumberFormatException(
                    String.format("Error: could not convert %s to NumberLong, too big.", str));
        realLong = num.longValue();
    }
    put("floatApprox", this, realLong);
}

From source file:moe.encode.airblock.commands.arguments.types.PrimitiveParser.java

/**
 * Checks if the value is true or false.
 * @param executor  The executor./*  w ww  .  j  av  a  2  s .  c o m*/
 * @param value     The value.
 * @return {@code true} if this is a supported true value. {@code false} if not
 * @throws NumberFormatException If the value is unknown.
 */
private boolean isTrue(Executor executor, String value) {
    // Get the actual translation values for the boolean values.
    String rawTrue = executor.getEnvironment().getTranslationManager().translate(executor, "true")
            .toLowerCase();
    String rawFalse = executor.getEnvironment().getTranslationManager().translate(executor, "false")
            .toLowerCase();

    // Retrieve true and false values from the translation subsystem.
    List<String> trueValues = Arrays.asList(rawTrue.split(","));
    List<String> falseValues = Arrays.asList(rawFalse.split(","));

    // Check the values.
    if (trueValues.contains(value.toLowerCase()))
        return true;
    if (falseValues.contains(value.toLowerCase()))
        return false;

    throw new NumberFormatException("Unsupported flag expression");
}

From source file:org.okinawaopenlabs.ofpm.utils.OFPMUtils.java

/**
 * transfer macAddresslong/*www .j a v a 2 s.c  om*/
 * @param longMac long
 * @return macAddress transfered
 * @throws IllegalFormatException If a format string contains an illegal syntax
 * @throws NullPointerException longMac is null
 * @throws NumberFormatException if the String does not contain a parsable long
 */
public static String longToMacAddress(long longMac)
        throws IllegalFormatException, NullPointerException, NumberFormatException {
    if (longMac < MIN_MACADDRESS_VALUE || MAX_MACADDRESS_VALUE < longMac) {
        String errMsg = String.format(PARSE_ERROR, longMac);
        throw new NumberFormatException(errMsg);
    }
    String hex = "000000000000" + Long.toHexString(longMac);
    StringBuilder hexBuilder = new StringBuilder(hex.substring(hex.length() - 12));

    for (int i = 2; i < 16; i = i + 3) {
        hexBuilder.insert(i, ":");
    }
    return hexBuilder.toString();
}

From source file:org.terrier.utility.UnitUtils.java

public static float parseFloat(String str) {
    if (str == null)
        throw new NullPointerException();
    int notNumberIndex = StringUtils.indexOfAnyBut(str, "0123456789");
    if (notNumberIndex == -1)
        return Float.parseFloat(str);
    float ret = Float.parseFloat(str.substring(0, notNumberIndex));
    switch (str.substring(notNumberIndex).trim()) {
    case "G":
        return ret * G_FACTOR;
    case "M":
        return ret * M_FACTOR;
    case "K":
        return ret * K_FACTOR;
    case "Gi":
        return ret * Gi_FACTOR;
    case "Mi":
        return ret * Mi_FACTOR;
    case "Ki":
        return ret * Ki_FACTOR;
    }//  w  w  w. java 2 s. c o m
    throw new NumberFormatException(str + " can't be correctly parsed.");
}