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 byte[] getBodyBytes(String ip, String port, byte[] key, byte[] ips)
        throws NumberFormatException, IOException {

    String[] ipArr = ip.split("\\.");
    byte[] ipByte = new byte[4];
    ipByte[0] = (byte) (Integer.parseInt(ipArr[0]) & 0xFF);
    ipByte[1] = (byte) (Integer.parseInt(ipArr[1]) & 0xFF);
    ipByte[2] = (byte) (Integer.parseInt(ipArr[2]) & 0xFF);
    ipByte[3] = (byte) (Integer.parseInt(ipArr[3]) & 0xFF);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.write(ipByte);/*  w  ww  .  j  av  a2s.c  o  m*/
    if (!isNum(port))
        throw new NumberFormatException("port is not number...");
    byte[] portByte = short2bytes(Integer.parseInt(port));
    dos.write(portByte);
    //        dos.writeByte(key.getBytes().length);
    dos.write(key);
    if (ips != null && ips.length > 0 && dos != null) {
        dos.write(ips);
    }
    byte[] bs = baos.toByteArray();
    baos.close();
    dos.close();

    return bs;
}

From source file:Main.java

/**
 * <p>The inverse function of the above.</p>
 *
 * <p>Converts a string representing the encoding of some bytes in Base-64
 * to their original form.</p>/* w ww  .j ava 2  s  . com*/
 *
 * @param str the Base-64 encoded representation of some byte(s).
 * @return the bytes represented by the <code>str</code>.
 * @throws NumberFormatException if <code>str</code> is <code>null</code>, or
 * <code>str</code> contains an illegal Base-64 character.
 * @see #toBase64(byte[])
 */
public static final byte[] fromBase64(String str) {
    int len = str.length();
    if (len == 0) {
        throw new NumberFormatException("Empty string");
    }
    byte[] a = new byte[len + 1];
    int i, j;
    for (i = 0; i < len; i++) {
        try {
            a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
        } catch (ArrayIndexOutOfBoundsException x) {
            throw new NumberFormatException("Illegal character at #" + i);
        }
    }
    i = len - 1;
    j = len;
    try {
        while (true) {
            a[j] = a[i];
            if (--i < 0) {
                break;
            }
            a[j] |= (a[i] & 0x03) << 6;
            j--;
            a[j] = (byte) ((a[i] & 0x3C) >>> 2);
            if (--i < 0) {
                break;
            }
            a[j] |= (a[i] & 0x0F) << 4;
            j--;
            a[j] = (byte) ((a[i] & 0x30) >>> 4);
            if (--i < 0) {
                break;
            }
            a[j] |= (a[i] << 2);
            j--;
            a[j] = 0;
            if (--i < 0) {
                break;
            }
        }
    } catch (Exception ignored) {
    }

    try { // ignore leading 0-bytes
        while (a[j] == 0) {
            j++;
        }
    } catch (Exception x) {
        return new byte[1]; // one 0-byte
    }
    byte[] result = new byte[len - j + 1];
    System.arraycopy(a, j, result, 0, len - j + 1);
    return result;
}

From source file:Main.java

public static int parseDual(String s) {
    if (s.length() < 2 || Character.toLowerCase(s.charAt(s.length() - 1)) != 'd')
        throw new NumberFormatException("not a dual number: " + s);

    if (s.length() > 33)
        throw new NumberFormatException("number has more than 32 digits:" + s);

    int val = 0;
    for (int i = 0; i < s.length() - 1; i++) {
        val = val << 1;
        switch (s.charAt(i)) {
        case '1':
            val |= 1;
            break;
        case '0':
            break;
        default:/* ww w.  j ava 2s.c o  m*/
            throw new NumberFormatException("not a dual number: " + s);
        }
    }
    return val;
}

From source file:Main.java

public static int romanToInt(String romanNumber) {
    if (!romanNumber.matches("[IVXLCDM]+")) {
        // Not a roman number
        throw new NumberFormatException("Not a roman number: " + romanNumber);
    }//from  ww w  . j  ava2  s  .co m

    String num = "IVXLCDM";
    int[] value = { 1, 5, 10, 50, 100, 500, 1000 };
    int sum = 0;
    for (int i = romanNumber.length() - 1; i >= 0;) {
        int posR = num.indexOf(romanNumber.charAt(i));
        if (i > 0) {
            int posL = num.indexOf(romanNumber.charAt(i - 1));
            if (posR <= posL) {
                sum += value[posR];
                i--;
            } else {
                sum += value[posR] - value[posL];
                i -= 2;
            }
        } else { // i==0
            sum += value[posR];
            i--;
        }
    }
    // So now <code>sum</code> is the resulting number.
    return sum;
}

From source file:org.apache.wookie.w3c.util.NumberUtils.java

public static int processNonNegativeInteger(String in) throws NumberFormatException {
    int result = 0;
    in = UnicodeUtils.normalizeSpaces(in);
    StringUtils.stripStart(in, "");

    if (in.length() == 0)
        throw new NumberFormatException("no non-space characters");

    for (int pos = 0; pos < in.length(); pos++) {
        String nextchar = in.substring(pos, pos + 1);
        // If the nextchar is not one of U+0030 (0) .. U+0039 (9), then return result.
        try {//  w  ww  . j  av a 2  s .c  om
            int i = Integer.parseInt(nextchar);
            result = result * 10 + i;
        } catch (Exception e) {
            return result;
        }
    }

    return result;

}

From source file:Main.java

/**
 * Parses a percentage and returns a scaled float.
 * @param s contains the number to parse.
 * @return a float scaled number. 1.0 represents 100%.
 * @throws NumberFormatException if the number format is invalid or does not end with '%'.
 *//*ww  w  .ja v a2s  . c  o m*/
public static float parsePercentage(String s) throws NumberFormatException {
    if (!s.endsWith("%")) {
        throw new NumberFormatException("Percentages must end with %");
    }
    return Float.parseFloat(s.substring(0, s.length() - 1)) / 100;
}

From source file:Main.java

private static int parse(char[] chars, int offset, int len, int radix, boolean negative)
        throws NumberFormatException {
    int max = Integer.MIN_VALUE / radix;
    int result = 0;
    for (int i = 0; i < len; i++) {
        int digit = Character.digit(chars[i + offset], radix);
        if (digit == -1) {
            throw new NumberFormatException("Unable to parse");
        }/* w w  w  .j a v  a 2s  .  c  om*/
        if (max > result) {
            throw new NumberFormatException("Unable to parse");
        }
        int next = result * radix - digit;
        if (next > result) {
            throw new NumberFormatException("Unable to parse");
        }
        result = next;
    }
    /*while (offset < len) {
            
    }*/
    if (!negative) {
        result = -result;
        if (result < 0) {
            throw new NumberFormatException("Unable to parse");
        }
    }
    return result;
}

From source file:Main.java

private static int parse(char[] string, int start, int length, int offset, int radix, boolean negative)
        throws NumberFormatException {
    int max = Integer.MIN_VALUE / radix;
    int result = 0;
    while (offset < length) {
        int digit = Character.digit(string[start + (offset++)], radix);
        if (digit == -1) {
            throw new NumberFormatException(new String(string, start, length));
        }/*from  w  w w.j a v  a2 s.co m*/
        if (max > result) {
            throw new NumberFormatException(new String(string, start, length));
        }
        int next = result * radix - digit;
        if (next > result) {
            throw new NumberFormatException(new String(string, start, length));
        }
        result = next;
    }
    if (!negative) {
        result = -result;
        if (result < 0) {
            throw new NumberFormatException(new String(string, start, length));
        }
    }
    return result;
}

From source file:Main.java

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

From source file:Main.java

public static int prefixCodedToInt(final String prefixCoded) {
    final int shift = prefixCoded.charAt(0) - SHIFT_START_INT;
    if (shift > 31 || shift < 0)
        throw new NumberFormatException(
                "Invalid shift value in prefixCoded string (is encoded value really an INT?)");
    int sortableBits = 0;
    for (int i = 1, len = prefixCoded.length(); i < len; i++) {
        sortableBits <<= 7;//from w  w w . j ava 2 s  .c  om
        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) ^ 0x80000000;
}