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:op.care.nursingprocess.PnlSchedule.java

public void save() throws NumberFormatException {

    boolean splitSetToTime = splitRegularPos == 0d;

    if (!isAtLeastOneTxtFieldNotZero() && Double.parseDouble(txtUhrzeit.getText()) == 0d) {
        throw new NumberFormatException("Anzahl der Durchfhrungen steht auf null.");
    }/*from ww w  . ja  va 2s. com*/

    is.setNachtMo(splitSetToTime ? (short) 0 : Short.parseShort(txtNachtMo.getText()));
    is.setMorgens(splitSetToTime ? (short) 0 : Short.parseShort(txtMorgens.getText()));
    is.setMittags(splitSetToTime ? (short) 0 : Short.parseShort(txtMittags.getText()));
    is.setNachmittags(splitSetToTime ? (short) 0 : Short.parseShort(txtNachmittags.getText()));
    is.setAbends(splitSetToTime ? (short) 0 : Short.parseShort(txtAbends.getText()));
    is.setNachtAb(splitSetToTime ? (short) 0 : Short.parseShort(txtNachtAb.getText()));
    is.setUhrzeitAnzahl(!splitSetToTime ? (short) 0 : Short.parseShort(txtUhrzeit.getText()));
    is.setUhrzeit(!splitSetToTime ? null : (Date) cmbUhrzeit.getSelectedItem());

    is.setTaeglich(tabWdh.getSelectedIndex() == TAB_DAILY ? Short.parseShort(spinTaeglich.getValue().toString())
            : (short) 0);
    is.setWoechentlich(
            tabWdh.getSelectedIndex() == TAB_WEEKLY ? Short.parseShort(spinWoche.getValue().toString())
                    : (short) 0);
    is.setMonatlich(tabWdh.getSelectedIndex() == TAB_MONTHLY ? Short.parseShort(spinMonat.getValue().toString())
            : (short) 0);

    DateMidnight day;
    try {
        day = new DateMidnight(SYSCalendar.parseDate(txtLDate.getText()));
    } catch (NumberFormatException ex) {
        day = new DateMidnight();
    }
    is.setLDatum(day.toDate());

    is.setMon(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbMon.isSelected() ? (short) 1 : (short) 0);
    is.setDie(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbDie.isSelected() ? (short) 1 : (short) 0);
    is.setMit(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbMit.isSelected() ? (short) 1 : (short) 0);
    is.setDon(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbDon.isSelected() ? (short) 1 : (short) 0);
    is.setFre(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbFre.isSelected() ? (short) 1 : (short) 0);
    is.setSam(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbSam.isSelected() ? (short) 1 : (short) 0);
    is.setSon(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbSon.isSelected() ? (short) 1 : (short) 0);

    if (tabWdh.getSelectedIndex() == TAB_MONTHLY) {
        short s = Short.parseShort(spinMonatTag.getValue().toString());
        is.setTagNum(cmbTag.getSelectedIndex() == 0 ? s : (short) 0);

        if (cmbTag.getSelectedIndex() == 1) {
            is.setMon(s);
        } else if (cmbTag.getSelectedIndex() == 2) {
            is.setDie(s);
        } else if (cmbTag.getSelectedIndex() == 3) {
            is.setMit(s);
        } else if (cmbTag.getSelectedIndex() == 4) {
            is.setDon(s);
        } else if (cmbTag.getSelectedIndex() == 5) {
            is.setFre(s);
        } else if (cmbTag.getSelectedIndex() == 6) {
            is.setSam(s);
        } else if (cmbTag.getSelectedIndex() == 7) {
            is.setSon(s);
        }
    }

    is.setFloating(tbFloating.isSelected());
    is.setBemerkung(txtBemerkung.getText());
    is.setDauer(SYSTools.parseDecimal(txtMinutes.getText()));

    if (!is.isValid()) {
        throw new NumberFormatException("Anzahl muss min. 1 sein");
    }

}

From source file:op.care.prescription.PnlScheduleDose.java

public void save() throws NumberFormatException {

    boolean splitSetToTime = splitRegularPos == 0d;

    if (!isAtLeastOneTxtFieldNotZero() && Double.parseDouble(txtTimeDose.getText()) == 0d) {
        throw new NumberFormatException("All Doses are ZERO");
    }/*from w w w.j  a  va2  s.  c o  m*/

    schedule.setNachtMo(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtVeryEarly.getText())));
    schedule.setMorgens(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtMorning.getText())));
    schedule.setMittags(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtNoon.getText())));
    schedule.setNachmittags(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtAfternoon.getText())));
    schedule.setAbends(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtEvening.getText())));
    schedule.setNachtAb(
            splitSetToTime ? BigDecimal.ZERO : new BigDecimal(Double.parseDouble(txtVeryLate.getText())));
    schedule.setUhrzeitDosis(
            splitSetToTime ? new BigDecimal(Double.parseDouble(txtTimeDose.getText())) : BigDecimal.ZERO);
    schedule.setUhrzeit(splitSetToTime ? (Date) cmbUhrzeit.getSelectedItem() : null);

    schedule.setMaxAnzahl(0);
    schedule.setMaxEDosis(BigDecimal.ZERO);

    schedule.setTaeglich(
            tabWdh.getSelectedIndex() == TAB_DAILY ? Short.parseShort(txtEveryDay.getText()) : (short) 0);
    schedule.setWoechentlich(
            tabWdh.getSelectedIndex() == TAB_WEEKLY ? Short.parseShort(txtEveryWeek.getText()) : (short) 0);
    schedule.setMonatlich(
            tabWdh.getSelectedIndex() == TAB_MONTHLY ? Short.parseShort(txtEveryMonth.getText()) : (short) 0);

    DateMidnight day;
    try {
        day = new DateMidnight(SYSCalendar.parseDate(txtLDate.getText()));
    } catch (NumberFormatException ex) {
        day = new DateMidnight();
    }
    schedule.setLDatum(day.toDate());

    schedule.setMon(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbMon.isSelected() ? (short) 1 : (short) 0);
    schedule.setTue(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbTue.isSelected() ? (short) 1 : (short) 0);
    schedule.setWed(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbWed.isSelected() ? (short) 1 : (short) 0);
    schedule.setThu(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbThu.isSelected() ? (short) 1 : (short) 0);
    schedule.setFri(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbFri.isSelected() ? (short) 1 : (short) 0);
    schedule.setSat(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbSat.isSelected() ? (short) 1 : (short) 0);
    schedule.setSun(tabWdh.getSelectedIndex() == TAB_WEEKLY && cbSun.isSelected() ? (short) 1 : (short) 0);

    if (tabWdh.getSelectedIndex() == TAB_MONTHLY) {
        short s = Short.parseShort(txtEveryWDayOfMonth.getText());
        schedule.setTagNum(cmbWDay.getSelectedIndex() == 0 ? s : (short) 0);

        if (cmbWDay.getSelectedIndex() == 1) {
            schedule.setMon(s);
        } else if (cmbWDay.getSelectedIndex() == 2) {
            schedule.setTue(s);
        } else if (cmbWDay.getSelectedIndex() == 3) {
            schedule.setWed(s);
        } else if (cmbWDay.getSelectedIndex() == 4) {
            schedule.setThu(s);
        } else if (cmbWDay.getSelectedIndex() == 5) {
            schedule.setFri(s);
        } else if (cmbWDay.getSelectedIndex() == 6) {
            schedule.setSat(s);
        } else if (cmbWDay.getSelectedIndex() == 7) {
            schedule.setSun(s);
        }
    }

}

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

/**
 * Convert a number into base-26 using lower case letters. Negative numbers
 * are not supported; only the magnitude in base-26 will be returned.
 * Adapted from http://en.wikipedia.org/wiki/Hexavigesimal.
 *
 * @param number a non-negative number/*from   ww  w  .j a va2 s  .  com*/
 * @return a string representing the number in base 26 using lower case
 * @throws NumberFormatException if the number is negative
 */
public static String toBase26(int number) throws NumberFormatException {
    if (number < 0)
        throw new NumberFormatException("Negative number");
    number = Math.abs(number);
    String converted = "";
    // Repeatedly divide the number by 26 and convert the
    // remainder into the appropriate letter.
    do {
        int remainder = number % 26;
        converted = (char) (remainder + 'a') + converted;
        number = (number - remainder) / 26;
    } while (number > 0);
    return converted;
}

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

/**
 * Convert a base-26 representation into a number, considering it to represent
 * a value in base-26 using the characters a-z or A-Z - the string is treated
 * case-insensitively. The string is lower cased before the increment is
 * applied. An exception will be thrown if any character is outside of a-z
 * after lower casing.//from   w w w . ja va  2 s  . com
 * <p>
 * Adapted from http://en.wikipedia.org/wiki/Hexavigesimal.
 * @param number a string representing a base-26 number
 * @return the number represented
 * @throws NumberFormatException if the string does not represent an alphabetical base-26 number
 */
public static int fromBase26(String number) throws NumberFormatException {
    if (!number.matches("^[a-zA-Z]*$"))
        throw new NumberFormatException("Non-alphabetic characters in string.");

    int s = 0;
    if (number != null && number.length() > 0) {
        number = number.toLowerCase();
        s = (number.charAt(0) - 'a');
        for (int i = 1; i < number.length(); i++) {
            s *= 26;
            s += (number.charAt(i) - 'a');
        }
    }
    return s;
}

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

/** Parse a string as a time interval.  An interval is specified as an
 * integer with an optional suffix.  No suffix means milliseconds, s, m,
 * h, d, w indicates seconds, minutes, hours, days and weeks
 * respectively.  As a special case, "ms" means milliseconds.
 * @param str the interval string//ww  w .  j a  va 2s. c o  m
 * @return interval in milliseconds
 */
// tk - extend to accept combinations: xxHyyMzzS, etc.
public static long parseTimeInterval(String str) {
    try {
        int len = str.length();
        char suffix = str.charAt(len - 1);
        String numstr;
        long mult = 1;
        if (Character.isDigit(suffix)) {
            numstr = str;
        } else {
            if (StringUtil.endsWithIgnoreCase(str, "ms")) {
                numstr = str.substring(0, len - 2);
            } else {
                numstr = str.substring(0, len - 1);
                switch (Character.toUpperCase(suffix)) {
                case 'S':
                    mult = Constants.SECOND;
                    break;
                case 'M':
                    mult = Constants.MINUTE;
                    break;
                case 'H':
                    mult = Constants.HOUR;
                    break;
                case 'D':
                    mult = Constants.DAY;
                    break;
                case 'W':
                    mult = Constants.WEEK;
                    break;
                case 'Y':
                    mult = Constants.YEAR;
                    break;
                default:
                    throw new NumberFormatException("Illegal time interval suffix");
                }
            }
        }
        return Long.parseLong(numstr) * mult;
    } catch (IndexOutOfBoundsException e) {
        throw new NumberFormatException("empty string");
    }
}

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

/** Parse a string as a size in bytes, with a optional suffix.  No suffix
 * means bytes, kb, mb, gb, tb indicate kilo-, mega-, giga, tera-bytes
 * respectively.//from w  w  w .  j  a  v a2  s. c  o m
 * @param str the size string
 * @return size in bytes
 */
public static long parseSize(String str) {
    str = str.trim();
    Perl5Matcher matcher = RegexpUtil.getMatcher();
    if (!matcher.contains(str, sizePat)) {
        throw new NumberFormatException("Illegal size syntax: " + str);
    }
    MatchResult matchResult = matcher.getMatch();
    String num = matchResult.group(1);
    String suffix = matchResult.group(2);
    try {
        float f = Float.parseFloat(num);
        long mult = 1;
        if (StringUtil.isNullString(suffix)) {
            return (long) f;
        }
        for (int ix = 0; ix < suffixes.length; ix++) {
            if (suffix.equalsIgnoreCase(suffixes[ix])) {
                return (long) (f * mult);
            }
            mult *= 1024;
        }
        throw new NumberFormatException("Illegal size suffix: " + str);
    } catch (NumberFormatException ex) {
        throw new NumberFormatException("Illegal size syntax: " + str);
    }
}

From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java

private static int parseSignedInt(String str) {
    if (str == null)
        throw new NumberFormatException("null is not a number");
    int len = str.length();
    if (len == 0)
        throw new NumberFormatException("empty string is not a number");
    int num = 0;//from www.j av a2s. co  m
    if (str.charAt(0) == '+') {
        if (len == 1)
            throw new NumberFormatException("+ is not a number");
        num = Integer.parseInt(str.substring(1));
    } else {
        num = Integer.parseInt(str);
    }
    return num;
}

From source file:com.zimbra.cs.mailbox.calendar.ZRecur.java

private static int parseUnsignedInt(String str) {
    if (str == null)
        throw new NumberFormatException("null is not a number");
    int len = str.length();
    if (len == 0)
        throw new NumberFormatException("empty string is not a number");
    char sign = str.charAt(0);
    if (sign == '+' || sign == '-')
        throw new NumberFormatException("sign not allowed: " + str);
    return Integer.parseInt(str);
}

From source file:mondrian.olap.Util.java

/**
 * Parses an interval./*  www  .j av  a2 s . c om*/
 *
 * <p>For example, "30s" becomes (30, {@link TimeUnit#SECONDS});
 * "2us" becomes (2, {@link TimeUnit#MICROSECONDS}).</p>
 *
 * <p>Units m (minutes), h (hours) and d (days) are only available
 * in JDK 1.6 or later, because the corresponding constants are missing
 * from {@link TimeUnit} in JDK 1.5.</p>
 *
 * @param s String to parse
 * @param unit Default time unit; may be null
 *
 * @return Pair of value and time unit. Neither pair or its components are
 * null
 *
 * @throws NumberFormatException if unit is not present and there is no
 * default, or if number is not valid
 */
public static Pair<Long, TimeUnit> parseInterval(String s, TimeUnit unit) throws NumberFormatException {
    final String original = s;
    for (Map.Entry<String, String> entry : TIME_UNITS.entrySet()) {
        final String abbrev = entry.getKey();
        if (s.endsWith(abbrev)) {
            final String full = entry.getValue();
            try {
                unit = TimeUnit.valueOf(full);
                s = s.substring(0, s.length() - abbrev.length());
                break;
            } catch (IllegalArgumentException e) {
                // ignore - MINUTES, HOURS, DAYS are not defined in JDK1.5
            }
        }
    }
    if (unit == null) {
        throw new NumberFormatException("Invalid time interval '" + original + "'. Does not contain a "
                + "time unit. (Suffix may be ns (nanoseconds), "
                + "us (microseconds), ms (milliseconds), s (seconds), "
                + "h (hours), d (days). For example, '20s' means 20 seconds.)");
    }
    try {
        return Pair.of(new BigDecimal(s).longValue(), unit);
    } catch (NumberFormatException e) {
        throw new NumberFormatException("Invalid time interval '" + original + "'");
    }
}

From source file:net.pms.util.Rational.java

/**
 * Converts this {@link Rational} to a {@link BigDecimal} using the given
 * {@link MathContext}. This may involve rounding.
 * <p>/* w  ww . j  av a 2s . c om*/
 *
 * @param mathContext the {@link MathContext} to use.
 * @return This {@link Rational} converted to a {@link BigDecimal}.
 * @throws ArithmeticException If this is {@code NaN} or infinite or if the
 *             result is inexact but the rounding mode is
 *             {@code UNNECESSARY} or {@code mathContext.precision == 0} and
 *             the quotient has a non-terminating decimal expansion.
 *
 * @see #bigDecimalValue()
 * @see #bigDecimalValue(RoundingMode)
 * @see #bigDecimalValue(int, RoundingMode)
 */
@Nonnull
public BigDecimal bigDecimalValue(MathContext mathContext) {
    if (isNaN()) {
        throw new NumberFormatException("Impossible to express NaN as BigDecimal");
    }
    if (isInfinite()) {
        throw new NumberFormatException("Impossible to express infinity as BigDecimal");
    }

    if (BigInteger.ONE.equals(reducedDenominator)) {
        return new BigDecimal(reducedNumerator);
    }
    return new BigDecimal(reducedNumerator).divide(new BigDecimal(reducedDenominator), mathContext);
}