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:Main.java

public static String formatValue(double value) {
    if (value < 0) {
        throw new NumberFormatException("Negative value " + value);
    }//from   w w  w.j  av  a  2  s.  co m
    String s = String.format(Locale.US, "%.8f", value);
    while (s.length() > 1 && (s.endsWith("0") || s.endsWith("."))) {
        s = (s.substring(0, s.length() - 1));
    }
    return s;
}

From source file:org.cryptomath.util.NumberUtil.java

public static String absorbFloats(String value) {
    String result;//from  w  w  w  . j  a  va 2  s. c  om
    if (value.matches("[\\d]*.[\\d]*")) {
        logger.info("Absorbing floats");
        BigDecimal bd = new BigDecimal(value);
        int f = CryptoConfigSpec.getInstance().getMathConfig().getFractions();
        //       logger.info("Absorbed value::check" + value);
        //       logger.info("Absorbed value::check" + bd);
        bd = bd.multiply(new BigDecimal("10").pow(f));
        //            logger.info("Absorbed value::check" + bd);
        String[] tokens = bd.toString().split("\\.");
        result = tokens[0];
        //       result = truncate(value, f).toString();
        logger.info("Absorbed value::check::" + result);
    } else {
        throw new NumberFormatException(MessageFormat.format("Invalid argument {0}", value));
    }
    return result;
}

From source file:Main.java

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

From source file:Main.java

/**
 * This function decodes a base 2 string into its corresponding byte array.
 *
 * @param base2 The base 2 encoded string.
 * @return The corresponding byte array.
 *///w  ww. ja va 2  s.  c om
static public byte[] decode(String base2) {
    String string = base2.replaceAll("\\s", ""); // remove all white space
    int length = string.length();
    byte[] bytes = new byte[(int) Math.ceil(length / 8)];
    for (int i = 0; i < bytes.length; i++) {
        int b = 0;
        for (int j = 0; j < 8; j++) {
            char character = string.charAt(i * 8 + j);
            int bit = lookupTable.indexOf(character);
            if (bit < 0)
                throw new NumberFormatException("Attempted to decode a string that is not base 2: " + string);
            b = (b << 1) | bit;
        }
        bytes[i] = (byte) b;
    }
    return bytes;
}

From source file:org.ScripterRon.BitcoinMonitor.Utils.java

/**
 * Parse a hex string and return the decoded bytes
 *
 * @param       hex                     String to parse
 * @return                              Decoded bytes
 * @throws      NumberFormatException   String contains an invalid hex character
 *//*from  w  w  w .  j a  va2  s  .  co m*/
public static byte[] parseHexString(String hex) throws NumberFormatException {
    if ((hex.length() & 0x01) == 1)
        throw new NumberFormatException("Hex string length is not a multiple of 2");
    byte[] bytes = new byte[hex.length() / 2];
    for (int i = 0; i < bytes.length; i++) {
        int char1 = hex.charAt(i * 2);
        char1 = (char1 > 0x60 ? char1 - 0x57 : char1 - 0x30);
        int char2 = hex.charAt(i * 2 + 1);
        char2 = (char2 > 0x60 ? char2 - 0x57 : char2 - 0x30);
        if (char1 < 0 || char2 < 0 || char1 > 15 || char2 > 15)
            throw new NumberFormatException("Invalid hex number: " + hex);
        bytes[i] = (byte) ((char1 << 4) + char2);
    }
    return bytes;
}

From source file:Main.java

public static long prefixCodedToLong(final String prefixCoded) {
    final int shift = prefixCoded.charAt(0) - SHIFT_START_LONG;
    if (shift > 63 || shift < 0)
        throw new NumberFormatException(
                "Invalid shift value in prefixCoded string (is encoded value really a LONG?)");
    long sortableBits = 0L;
    for (int i = 1, len = prefixCoded.length(); i < len; i++) {
        sortableBits <<= 7;/*from  w w  w .  j a v a2  s .co m*/
        final char ch = prefixCoded.charAt(i);
        if (ch > 0x7f) {
            throw new NumberFormatException("Invalid prefixCoded numerical value representation (char "
                    + Integer.toHexString(ch) + " at position " + i + " is invalid)");
        }
        sortableBits |= ch;
    }
    return (sortableBits << shift) ^ 0x8000000000000000L;
}

From source file:Main.java

public static String formatValue(long value) throws NumberFormatException {
    if (value < 0) {
        throw new NumberFormatException("Negative value " + value);
    }/*from   w w w .  j  a  v  a2s. c om*/
    StringBuilder sb = new StringBuilder(Long.toString(value));
    while (sb.length() <= 8) {
        sb.insert(0, '0');
    }
    sb.insert(sb.length() - 8, '.');
    while (sb.length() > 1 && (sb.charAt(sb.length() - 1) == '0' || sb.charAt(sb.length() - 1) == '.')) {
        sb.setLength(sb.length() - 1);
    }
    return sb.toString();
}

From source file:com.rackspacecloud.blueflood.utils.Util.java

public static Collection<Integer> parseShards(String s) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    if ("ALL".equalsIgnoreCase(s)) {
        for (int i = 0; i < Constants.NUMBER_OF_SHARDS; i++)
            list.add(i);//  w  w  w .j  av a 2s . com
    } else if ("NONE".equalsIgnoreCase(s)) {
        return list;
    } else {
        for (String part : s.split(",", -1)) {
            int i = Integer.parseInt(part.trim());
            if (i >= Constants.NUMBER_OF_SHARDS || i < 0)
                throw new NumberFormatException("Invalid shard identifier: " + part.trim());
            list.add(i);
        }
    }
    return list;
}

From source file:com.pokercompany.stringcalc.StringCalculator.java

public int add(String numbers) throws Exception {
    if (numbers == null) {
        throw new NullPointerException();
    }/*  w w w.j a v  a2 s.  c  o m*/

    if (numbers.equals("")) {
        return 0;
    }

    if (!numbers.matches("-?[0-9]+([" + separator + "]-?[0-9]+)*[" + separator + "]?")) {
        throw new NumberFormatException("Bad character(s) in input string: " + numbers);
    }

    List<String> numbersList = Arrays.asList(numbers.split("(?<!\\" + separator + ")[" + separator + "]"));
    List<String> negativeNumbers = StringCalcUtil.getListWherePatternMatches(numbersList, "[-][0-9]+");

    if (negativeNumbers.size() > 0) {
        throw new Exception("Negative not allowed: " + StringUtils.join(negativeNumbers.iterator(), ","));
    }

    int sum = 0;
    for (String stringNumber : numbersList) {
        sum += Integer.valueOf(stringNumber);
    }

    return sum;
}

From source file:org.cryptomath.util.NumberUtil.java

public static String spillFloats(String value, final int pTimes) {
    String result;//  ww  w. jav  a2  s.  c  o m
    if (value.matches("[\\d]*")) {

        BigDecimal bd = new BigDecimal(value);
        int f = CryptoConfigSpec.getInstance().getMathConfig().getFractions();
        bd = bd.divide(new BigDecimal("10").pow(f * pTimes));

        result = bd.toString();
    } else {
        throw new NumberFormatException(MessageFormat.format("Invalid argument {0}", value));
    }
    return result;
}