Example usage for java.lang Character isDigit

List of usage examples for java.lang Character isDigit

Introduction

In this page you can find the example usage for java.lang Character isDigit.

Prototype

public static boolean isDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a digit.

Usage

From source file:com.jklas.sample.petclinic.validation.OwnerValidator.java

public void validate(Object obj, Errors errors) {
    Owner owner = (Owner) obj;
    ValidationUtils.rejectIfEmpty(errors, "firstName", "required", "required");
    ValidationUtils.rejectIfEmpty(errors, "lastName", "required", "required");
    ValidationUtils.rejectIfEmpty(errors, "address", "required", "required");
    ValidationUtils.rejectIfEmpty(errors, "city", "required", "required");
    String telephone = owner.getTelephone();
    if (!StringUtils.hasLength(telephone)) {
        errors.rejectValue("telephone", "required", "required");
    } else {/* w  w  w. ja va  2 s .  com*/
        for (int i = 0; i < telephone.length(); ++i) {
            if ((Character.isDigit(telephone.charAt(i))) == false) {
                errors.rejectValue("telephone", "nonNumeric", "non-numeric");
                break;
            }
        }
    }
}

From source file:adalid.util.sql.SqlRow.java

/**
 * @return the row name//from  ww  w. ja  v  a2  s .  c o  m
 */
public String getJavaConstantName() {
    String name = getName();
    String prefix = name != null && Character.isDigit(name.charAt(0)) ? "_" : "";
    return prefix + StrUtils.getIdentificadorSqlUpperCase(name);
}

From source file:net.ripe.ipresource.Ipv4Address.java

public static Ipv4Address parse(String s, boolean defaultMissingOctets) {
    s = s.trim();/*  ww w  .ja v  a  2s  .  c  o  m*/

    int length = s.length();
    if (length == 0 || !Character.isDigit(s.charAt(0)) || !Character.isDigit(s.charAt(s.length() - 1))) {
        throw new IllegalArgumentException("invalid IPv4 address: " + s);
    }

    long value = 0;
    int octet = 0;
    int octetCount = 1;

    for (int i = 0; i < length; ++i) {
        char ch = s.charAt(i);
        if (Character.isDigit(ch)) {
            octet = octet * 10 + (ch - '0');
        } else if (ch == '.') {
            if (octetCount > 4) {
                throw new IllegalArgumentException("invalid IPv4 address: " + s);
            }
            octetCount++;

            value = addOctet(value, octet);

            octet = 0;
        } else {
            throw new IllegalArgumentException("invalid IPv4 address: " + s);
        }
    }

    value = addOctet(value, octet);

    if (defaultMissingOctets) {
        value <<= 8 * (4 - octetCount);
    } else if (octetCount != 4) {
        throw new IllegalArgumentException("invalid IPv4 address: " + s);
    }

    return new Ipv4Address(value);
}

From source file:DoubleDocument.java

/**
 * Strip all non digit characters.  The first character must be '-' or '+'.
 * Only one '.' is allowed./*from w  w  w. j  a v a 2  s  .c  om*/
 */
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {

    if (str == null) {
        return;
    }

    // Get current value
    String curVal = getText(0, getLength());
    boolean hasDot = curVal.indexOf('.') != -1;

    // Strip non digit characters
    char[] buffer = str.toCharArray();
    char[] digit = new char[buffer.length];
    int j = 0;

    if (offs == 0 && buffer != null && buffer.length > 0 && buffer[0] == '-')
        digit[j++] = buffer[0];

    for (int i = 0; i < buffer.length; i++) {
        if (Character.isDigit(buffer[i]))
            digit[j++] = buffer[i];
        if (!hasDot && buffer[i] == '.') {
            digit[j++] = '.';
            hasDot = true;
        }
    }

    // Now, test that new value is within range.
    String added = new String(digit, 0, j);
    try {
        StringBuffer val = new StringBuffer(curVal);
        val.insert(offs, added);
        String valStr = val.toString();
        if (valStr.equals(".") || valStr.equals("-") || valStr.equals("-."))
            super.insertString(offs, added, a);
        else {
            Double.valueOf(valStr);
            super.insertString(offs, added, a);
        }
    } catch (NumberFormatException e) {
        // Ignore insertion, as it results in an out of range value
    }
}

From source file:Main.java

/**
 * NameChar ::= NameStartChar | "-" | "." | [0-9]
 * @param charV//from w  w w .j av a2s.  c om
 * @return
 */
private static boolean isNameChar(char charV) {
    if (isStartChar(charV))
        return true;
    if (Character.isDigit(charV))
        return true;
    if (charV == '-')
        return true;
    if (charV == '.')
        return true;
    return false;
}

From source file:neembuu.httpserver.Utils.java

private static long findOffset(final HttpRequest request) {
    for (Header h : request.getAllHeaders()) {
        if (h.getName().trim().equalsIgnoreCase("Range")) {
            try {
                String val = h.getValue();
                int i = 0;
                SMALL_LOOP: for (; i < val.length(); i++) {
                    if (Character.isDigit(val.charAt(i))) {
                        break SMALL_LOOP;
                    }//from  ww w .j  a va2s. c  o m
                }
                val = val.substring(i, val.indexOf('-'));
                return Long.parseLong(val);
            } catch (Exception a) {
                a.printStackTrace();
            }
        }
    }
    return 0;
}

From source file:Main.java

/**
 * Parses a string as an opaque color, either as a decimal hue (example: {@code 330}) using a
 * standard set of saturation and value) or as six digit hex code (example: {@code #BB66FF}).
 * If the string cannot be parsed, a {@link ParseException} is thrown.
 *
 * @param str The input to parse./*from ww w . j a v  a2  s  .c o  m*/
 * @param tempHsvArray An optional previously allocated array for HSV calculations.
 * @return The parsed color, in {@code int} form.
 * @throws ParseException
 */
public static int parseColor(@NonNull String str, @Nullable float[] tempHsvArray) throws ParseException {
    Integer result = null;

    char firstChar = str.charAt(0);
    if (firstChar == '#' && str.length() == 7) {
        try {
            result = Integer.parseInt(str.substring(1, 7), 16);
        } catch (NumberFormatException e) {
            throw new ParseException("Invalid hex color: " + str, 0);
        }
        return result;
    } else if (Character.isDigit(firstChar) && str.length() <= 3) {
        try {
            int hue = Integer.parseInt(str);
            result = getBlockColorForHue(hue, tempHsvArray);
        } catch (NumberFormatException e) {
            throw new ParseException("Invalid color hue: " + str, 0);
        }
    }
    // Maybe other color formats? 3 digit hex, CSS color functions, etc.
    return result;
}

From source file:com.github.jferard.pgloaderutils.sniffer.csv.RowSignaturesAnalyzer.java

/**
 * @param s/*ww w.j a va2s.c  o m*/
 * @return the type of the value : 'D' for digits only value, 'd' for 80%
 *         digits values and at least one char, '?' else
 */
public char getType(final String s) {
    int digits = 0;
    int letters = 0;
    for (int i = 0; i < s.length(); i++) {
        final char c = s.charAt(i);
        if (Character.isLetter(c))
            letters++;
        else if (Character.isDigit(c))
            digits++;
        // else ignore
    }
    if (digits > 0) {
        if (letters == 0)
            return 'D';
        else if (digits >= 4 * letters)
            return 'd';
    }

    return '?';
}

From source file:org.apache.archiva.redback.policy.rules.NumericalPasswordRule.java

private int countDigitCharacters(String password) {
    int count = 0;

    if (StringUtils.isEmpty(password)) {
        return count;
    }//from w ww .  jav a2  s .co m

    /* TODO: Eventually upgrade to the JDK 1.5 Technique
     * 
     * // Doing this via iteration of code points to take in account localized numbers.
     * for ( int i = 0; i < password.length(); i++ )
     * {
     *     int codepoint = password.codePointAt( i );
     *     if ( Character.isDigit( codepoint ) )
     *     {
     *         count++;
     *     }
     * }
     */

    // JDK 1.4 Technique - NOT LOCALIZED.
    for (int i = 0; i < password.length(); i++) {
        char c = password.charAt(i);
        if (Character.isDigit(c)) {
            count++;
        }
    }

    return count;
}

From source file:Main.java

/**
 * Chunk digits or non-digits from a string starting at an index.
 *
 * @param s//from  ww  w  . j a  v a2 s.com
 * @param length
 * @param index
 * @return
 */
public static String getDigitOrNonDigitChunk(String s, int length, int index) {
    StringBuilder sb = new StringBuilder();
    char c = s.charAt(index);
    sb.append(c);
    index++;
    boolean digitOrNot = Character.isDigit(c);
    while (index < length) {
        c = s.charAt(index);
        if (digitOrNot != Character.isDigit(c)) {
            break;
        }
        sb.append(c);
        index++;
    }
    return sb.toString();
}