protected int checkNext(int pos, String s, int val, int type) throws ParseException {
    int end = -1;
    int i = pos;

    if (i >= s.length()) {
      addToSet(val, end, -1, type);
      return i;
    }

    char c = s.charAt(pos);

    if (c == 'L') {
      if (type == DAY_OF_WEEK) {
        lastdayOfWeek = true;
      } else {
        throw new ParseException(MSGS.cronOptionIsNotValidHere("L", Integer.toString(i)), i);
      }
      TreeSet set = getSet(type);
      set.add(new Integer(val));
      i++;
      return i;
    }

    if (c == 'W') {
      if (type == DAY_OF_MONTH) {
        nearestWeekday = true;
      } else {
        throw new ParseException(MSGS.cronOptionIsNotValidHere("W", Integer.toString(i)), i);
      }
      TreeSet set = getSet(type);
      set.add(new Integer(val));
      i++;
      return i;
    }

    if (c == '#') {
      if (type != DAY_OF_WEEK) {
        throw new ParseException(MSGS.cronOptionIsNotValidHere("#", Integer.toString(i)), i);
      }
      i++;
      try {
        nthdayOfWeek = Integer.parseInt(s.substring(i));
        if (nthdayOfWeek < 1 || nthdayOfWeek > 5) {
          throw new Exception();
        }
      } catch (Exception e) {
        throw new ParseException(MSGS.cronIllegalHashFollowingNumeric(), i);
      }

      TreeSet set = getSet(type);
      set.add(new Integer(val));
      i++;
      return i;
    }

    if (c == 'C') {
      if (type == DAY_OF_WEEK) {
        calendardayOfWeek = true;
      } else if (type == DAY_OF_MONTH) {
        calendardayOfMonth = true;
      } else {
        throw new ParseException(MSGS.cronOptionIsNotValidHere("C", Integer.toString(i)), i);
      }
      TreeSet set = getSet(type);
      set.add(new Integer(val));
      i++;
      return i;
    }

    if (c == '-') {
      i++;
      c = s.charAt(i);
      int v = Integer.parseInt(String.valueOf(c));
      end = v;
      i++;
      if (i >= s.length()) {
        addToSet(val, end, 1, type);
        return i;
      }
      c = s.charAt(i);
      if (c >= '0' && c <= '9') {
        ValueSet vs = getValue(v, s, i);
        int v1 = vs.value;
        end = v1;
        i = vs.pos;
      }
      if (i < s.length() && ((c = s.charAt(i)) == '/')) {
        i++;
        c = s.charAt(i);
        int v2 = Integer.parseInt(String.valueOf(c));
        i++;
        if (i >= s.length()) {
          addToSet(val, end, v2, type);
          return i;
        }
        c = s.charAt(i);
        if (c >= '0' && c <= '9') {
          ValueSet vs = getValue(v2, s, i);
          int v3 = vs.value;
          addToSet(val, end, v3, type);
          i = vs.pos;
          return i;
        } else {
          addToSet(val, end, v2, type);
          return i;
        }
      } else {
        addToSet(val, end, 1, type);
        return i;
      }
    }

    if (c == '/') {
      i++;
      c = s.charAt(i);
      int v2 = Integer.parseInt(String.valueOf(c));
      i++;
      if (i >= s.length()) {
        addToSet(val, end, v2, type);
        return i;
      }
      c = s.charAt(i);
      if (c >= '0' && c <= '9') {
        ValueSet vs = getValue(v2, s, i);
        int v3 = vs.value;
        addToSet(val, end, v3, type);
        i = vs.pos;
        return i;
      } else {
        throw new ParseException(MSGS.cronUnexpectedCharacterAfterSlash(String.valueOf(c)), i);
      }
    }

    addToSet(val, end, 0, type);
    i++;
    return i;
  }
  protected int storeExpressionVals(int pos, String s, int type) throws ParseException {
    int incr = 0;
    int i = skipWhiteSpace(pos, s);
    if (i >= s.length()) return i;
    char c = s.charAt(i);
    if ((c >= 'A') && (c <= 'Z') && (!s.equals("L")) && (!s.equals("LW"))) {
      String sub = s.substring(i, i + 3);
      int sval = -1;
      int eval = -1;
      if (type == MONTH) {
        sval = getMonthNumber(sub) + 1;
        if (sval < 0) {
          throw new ParseException(MSGS.cronInvalidMonthValue(sub), i);
        }
        if (s.length() > i + 3) {
          c = s.charAt(i + 3);
          if (c == '-') {
            i += 4;
            sub = s.substring(i, i + 3);
            eval = getMonthNumber(sub) + 1;
            if (eval < 0) {
              throw new ParseException(MSGS.cronInvalidMonthValue(sub), i);
            }
          }
        }
      } else if (type == DAY_OF_WEEK) {
        sval = getDayOfWeekNumber(sub);
        if (sval < 0) {
          throw new ParseException(MSGS.cronInvalidDOWValue(sub), i);
        }
        if (s.length() > i + 3) {
          c = s.charAt(i + 3);
          if (c == '-') {
            i += 4;
            sub = s.substring(i, i + 3);
            eval = getDayOfWeekNumber(sub);
            if (eval < 0) {
              throw new ParseException(MSGS.cronInvalidDOWValue(sub), i);
            }
            if (sval > eval) {
              throw new ParseException(
                  MSGS.cronInvalidDOWSequence(Integer.toString(sval), Integer.toString(eval)), i);
            }

          } else if (c == '#') {
            try {
              i += 4;
              nthdayOfWeek = Integer.parseInt(s.substring(i));
              if (nthdayOfWeek < 1 || nthdayOfWeek > 5) throw new Exception();
            } catch (Exception e) {
              throw new ParseException(MSGS.cronIllegalHashFollowingNumeric(), i);
            }
          } else if (c == 'L') {
            lastdayOfWeek = true;
            i++;
          }
        }

      } else {
        throw new ParseException(MSGS.cronIllegalCharactersForPosition(sub), i);
      }
      if (eval != -1) {
        incr = 1;
      }
      addToSet(sval, eval, incr, type);
      return (i + 3);
    }

    if (c == '?') {
      i++;
      if ((i + 1) < s.length() && (s.charAt(i) != ' ' && s.charAt(i + 1) != '\t')) {
        throw new ParseException(
            MSGS.cronIllegalCharacterAfter("?", String.valueOf(s.charAt(i))), i);
      }
      if (type != DAY_OF_WEEK && type != DAY_OF_MONTH) {
        throw new ParseException(MSGS.cronIllegalQuestionMark(), i);
      }
      if (type == DAY_OF_WEEK && !lastdayOfMonth) {
        int val = ((Integer) daysOfMonth.last()).intValue();
        if (val == NO_SPEC_INT) {
          throw new ParseException(MSGS.cronIllegalQuestionMark(), i);
        }
      }

      addToSet(NO_SPEC_INT, -1, 0, type);
      return i;
    }

    if (c == '*' || c == '/') {
      if (c == '*' && (i + 1) >= s.length()) {
        addToSet(ALL_SPEC_INT, -1, incr, type);
        return i + 1;
      } else if (c == '/'
          && ((i + 1) >= s.length() || s.charAt(i + 1) == ' ' || s.charAt(i + 1) == '\t')) {
        throw new ParseException(MSGS.cronIllegalSlash(), i);
      } else if (c == '*') {
        i++;
      }
      c = s.charAt(i);
      if (c == '/') { // is an increment specified?
        i++;
        if (i >= s.length()) {
          throw new ParseException(MSGS.cronUnexpectedEndOfString(), i);
        }

        incr = getNumericValue(s, i);

        i++;
        if (incr > 10) i++;
        if (incr > 59 && (type == SECOND || type == MINUTE)) {
          throw new ParseException(MSGS.cronIllegalIncrement("60", Integer.toString(incr)), i);
        } else if (incr > 23 && (type == HOUR)) {
          throw new ParseException(MSGS.cronIllegalIncrement("24", Integer.toString(incr)), i);
        } else if (incr > 31 && (type == DAY_OF_MONTH)) {
          throw new ParseException(MSGS.cronIllegalIncrement("31", Integer.toString(incr)), i);
        } else if (incr > 7 && (type == DAY_OF_WEEK)) {
          throw new ParseException(MSGS.cronIllegalIncrement("7", Integer.toString(incr)), i);
        } else if (incr > 12 && (type == MONTH)) {
          throw new ParseException(MSGS.cronIllegalIncrement("12", Integer.toString(incr)), i);
        }
      } else incr = 1;

      addToSet(ALL_SPEC_INT, -1, incr, type);
      return i;
    } else if (c == 'L') {
      i++;
      if (type == DAY_OF_MONTH) lastdayOfMonth = true;
      if (type == DAY_OF_WEEK) addToSet(7, 7, 0, type);
      if (type == DAY_OF_MONTH && s.length() > i) {
        c = s.charAt(i);
        if (c == 'W') {
          nearestWeekday = true;
          i++;
        }
      }
      return i;
    } else if (c >= '0' && c <= '9') {
      int val = Integer.parseInt(String.valueOf(c));
      i++;
      if (i >= s.length()) {
        addToSet(val, -1, -1, type);
      } else {
        c = s.charAt(i);
        if (c >= '0' && c <= '9') {
          ValueSet vs = getValue(val, s, i);
          val = vs.value;
          i = vs.pos;
        }
        i = checkNext(i, s, val, type);
        return i;
      }
    } else {
      throw new ParseException(MSGS.cronUnexpectedCharacter(String.valueOf(c)), i);
    }

    return i;
  }