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.jls.toolbox.util.ResourceManager.java

/**
 * Get an integer associated with the given configuration key. If the key
 * does not exist, an {@link IllegalArgumentException} is thrown. If the
 * value cannot be decoded using {@link Integer#parseInt(String)}, a
 * {@link NumberFormatException} is thrown.
 * //from w ww  .  ja  va  2  s  .  c o m
 * @param key
 *            The configuration key.
 * @return The associated value.
 */
public int getInt(final String key) {
    String str = getString(key);
    if (!str.isEmpty()) {
        try {
            return Integer.parseInt(str);
        } catch (@SuppressWarnings("unused") Exception e) {
            throw new NumberFormatException("Cannot parse value to integer : " + str);
        }
    }
    throw new IllegalStateException("Empty value");
}

From source file:com.opengamma.web.json.FudgeMsgJSONReader.java

private byte byteValue(final Object o) {
    if (o instanceof Number) {
        return ((Number) o).byteValue();
    } else if (o instanceof String) {
        return Byte.parseByte((String) o);
    } else {//w w  w  .  ja  v a 2 s .  c o m
        throw new NumberFormatException(o + " is not a number");
    }
}

From source file:no.abmu.questionnaire.domain.data.BigDecimalFieldData.java

@Transient
private void setValueAsString(String value) {
    //        logger.debug("Executing setValueAsString as String code=[" +  getCode() + "] value=[" + value + "]");
    if (value == null) {
        if (this.bigDecimalValue != null) {
            this.bigDecimalValue = null;
        }/*from  www .  j av a  2 s. com*/
        return;
    }
    String trimmedStringValue = value.trim();
    if (trimmedStringValue.isEmpty()) {
        if (this.bigDecimalValue != null) {
            this.bigDecimalValue = null;
        }
        return;
    }

    if (trimmedStringValue.contains(".")) {
        throw new NumberFormatException("Not valid number, use ',' not '.'");
    }

    DecimalFormat formatter = getNumberFormat();

    BigDecimal newBigDecimal;
    try {
        newBigDecimal = (BigDecimal) formatter.parse(trimmedStringValue);
    } catch (NumberFormatException e) {
        logger.error("Error on converting string value=[" + value + "]", e);
        throw new NumberFormatException(e.toString());
    } catch (ParseException e) {
        logger.error("Error on converting string value=[" + value + "]", e);
        throw new NumberFormatException(e.toString());
    }
    setBigDecimalValue(bigDecimalWithCorrectScaling(newBigDecimal));
}

From source file:de.pdark.dsmp.Config.java

private int getIntProperty(Element root, String element, String attribute, int defaultValue) {
    String value = getStringProperty(root, element, attribute, null);
    if (value == null)
        return defaultValue;

    try {//w w  w.  j a  va  2s.  co m
        return Integer.parseInt(value);
    } catch (NumberFormatException e) {
        throw (NumberFormatException) (new NumberFormatException("Error convertion value '" + value
                + "' of property " + element + "@" + attribute + ": " + e.getMessage()).initCause(e));
    }
}

From source file:com.jaspersoft.android.jaspermobile.activities.settings.fragment.SettingsFragment.java

private long toMillisValue(Object newValue) {
    int value = Integer.parseInt(String.valueOf(newValue));
    long millisValue = TimeUnit.SECONDS.toMillis(value);
    if (millisValue > Integer.MAX_VALUE) {
        throw new NumberFormatException("Timeout too large.");
    }//w  w w . j a v a  2  s .com
    return millisValue;
}

From source file:com.opengamma.web.json.FudgeMsgJSONReader.java

private short shortValue(final Object o) {
    if (o instanceof Number) {
        return ((Number) o).shortValue();
    } else if (o instanceof String) {
        return Short.parseShort((String) o);
    } else {//from  w ww . ja v a2  s .c o  m
        throw new NumberFormatException(o + " is not a number");
    }
}

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

/**
 * Generate a series of strings representing integers, covering the range from
 * the start string to the end string inclusive, and with zero-padding
 * matching the inputs. The start and end strings should be parseable as
 * integers, and if they contain zero padding should be of the same length.
 * The length of every string in the sequence will be maintained with
 * zero-padding if the start and end contain it.
 *
 * @param start//from w ww.ja  v  a  2  s.  c  om
 * @param end
 * @param delta the magnitude of the increment or decrement, expressed as a positive integer
 * @param pad
 * @return
 * @throws NumberFormatException if the start or end display zero-padding but are of different lengths
 */
protected static List<String> constructPaddedIntSequence(String start, String end, int delta)
        throws NumberFormatException {
    List<String> seq = new ArrayList<String>();
    int s = parseInt(start);
    int e = parseInt(end);
    int[] intSeq = constructSequence(s, e, delta);
    int padlen = 0;
    // If a number starts with a zero, maintain zero-padded length
    // in generated tokens.
    if (start.startsWith("0") || end.startsWith("0")) {
        padlen = start.length();
        // Check if the zero-padded numbers are the same length
        if (end.length() != padlen)
            throw new NumberFormatException(String.format(
                    "Can't generate sequence with different length " + "zero-padded numbers %s and %s.", start,
                    end));
    }
    // Zero-pad the numbers as necessary and add to result list
    for (int i : intSeq)
        seq.add(padNumbers(i, padlen));
    return seq;
}

From source file:Convert.java

/** 
 * Parses a number from a string./*from  ww w.  j  a  va 2s  .  c om*/
 * Finds the first recognizable base-10 number (integer or floating point)
 * in the string and returns it as a Number.
 * @param string String to parse
 * @return first recognizable number
 * @exception NumberFormatException if no recognizable number is found
 */
public static Number toNumber(String s) throws NumberFormatException {
    // parsing states
    int INT = 0;
    int FRAC = 1;
    int EXP = 2;
    int p = 0;
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (Character.isDigit(c)) {
            int start = i;
            int end = ++i;
            int state = INT;
            if (start > 0 && s.charAt(start - 1) == '.') {
                --start;
                state = FRAC;
            }
            if (start > 0 && s.charAt(start - 1) == '-')
                --start;
            boolean atEnd = false;
            while (!atEnd && i < s.length()) {
                switch (s.charAt(i)) {
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    end = ++i;
                    break;
                case '.':
                    if (state == INT) {
                        state = FRAC;
                        ++i;
                    } else {
                        atEnd = true;
                    }
                    break;
                case 'e':
                case 'E':
                    state = EXP;
                    ++i;
                    if (i < s.length() && ((c = s.charAt(i)) == '+' || c == '-'))
                        ++i;
                    break;
                default:
                    atEnd = true;
                }
            }
            String num = s.substring(start, end);
            try {
                if (state == INT)
                    return new Integer(num);
                else
                    return new Double(num);
            } catch (NumberFormatException e) {
                throw new RuntimeException("internal error: " + e);
            }
        }
    }
    throw new NumberFormatException(s);
}

From source file:org.carrot2.core.Document.java

/**
 * @deprecated please use {@link #getStringId()} instead. Currently, this method
 *             attempts to parse the string identifier returned by
 *             {@link #getStringId()} into an integer.
 * @throws NumberFormatException if the identifier could not be converted to an integer
 *             number//from  w  ww  .j  a v  a2s. com
 */
public Integer getId() {
    try {
        return id != null ? Integer.parseInt(id) : null;
    } catch (NumberFormatException e) {
        throw new NumberFormatException("Could not parse document identifier as an integer: " + id);
    }
}

From source file:org.openhab.ui.cometvisu.internal.util.ClientInstaller.java

/**
 * Compare two SemVer version strings and return true if releaseVersion is newer then currentVersion
 *
 * @param releaseVersion {String} e.g 0.11.0
 * @param currentVersion {String} e.g. 0.10.0
 * @return {Boolean}/* w  w  w.  java 2  s.c  om*/
 */
private boolean isNewer(String releaseVersion, String currentVersion) throws NumberFormatException {
    logger.debug("checking if {} is newer than {}", releaseVersion, currentVersion);
    Matcher release = semverPattern.matcher(releaseVersion);
    Matcher current = semverPattern.matcher(currentVersion);
    if (!release.matches()) {
        throw new NumberFormatException("release version format error " + releaseVersion);
    }
    if (!current.matches()) {
        throw new NumberFormatException("current version format error " + currentVersion);
    }

    for (int i = 1; i <= 3; i++) {
        if (Integer.parseInt(release.group(i)) > Integer.parseInt(current.group(i))) {
            return false;
        }
    }
    return true;
}