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:Main.java

/**
 * Performs the Luhn check on the given card number.
 *
 * @param cardNumber a String consisting of numeric digits (only).
 * @return {@code true} if the sequence passes the checksum
 * @throws IllegalArgumentException if {@code cardNumber} contained a non-digit (where {@link
 * Character#isDefined(char)} is {@code false}).
 * @see <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Luhn Algorithm (Wikipedia)</a>
 *//*from  w  w  w .jav a 2 s  .  c o m*/
public static boolean isLuhnValid(String cardNumber) {
    final String reversed = new StringBuffer(cardNumber).reverse().toString();
    final int len = reversed.length();
    int oddSum = 0;
    int evenSum = 0;
    for (int i = 0; i < len; i++) {
        final char c = reversed.charAt(i);
        if (!Character.isDigit(c)) {
            throw new IllegalArgumentException(String.format("Not a digit: '%s'", c));
        }
        final int digit = Character.digit(c, 10);
        if (i % 2 == 0) {
            oddSum += digit;
        } else {
            evenSum += digit / 5 + (2 * digit) % 10;
        }
    }
    return (oddSum + evenSum) % 10 == 0;
}

From source file:Main.java

/**
 * Determines if argument is a number// w w w .  jav a2  s  .co m
 * @param string - argument to test
 * @return - true if argument is a number
 */
public static boolean isNumber(String string) {
    boolean result = true;
    char[] chars = string.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        if (!Character.isDigit(chars[i])) {
            result = false;
        }
    }
    return result;
}

From source file:Main.java

public static byte[] fromHexString(String hexString) {
    char[] chars = hexString.toCharArray();
    byte[] result = new byte[chars.length / 2];
    for (int i = 0; i < result.length; i++) {
        char c = chars[2 * i];
        int a = ((Character.isDigit(c) ? c - '0' : c - 'A' + 10) << 4) & 0xF0;
        c = chars[2 * i + 1];//  ww w .j  a va  2s .c om
        int b = (Character.isDigit(c) ? c - '0' : c - 'A' + 10) & 0x0F;
        result[i] = (byte) ((a | b) & 0xFF);
    }
    return result;
}

From source file:Main.java

/**
 * Removes invalid char from element names.
 *//*from  w  ww . j a va 2  s.  c o m*/
public static String asElementName(String x) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < x.length(); ++i) {
        char c = x.charAt(i);
        if (Character.isUpperCase(c) || Character.isLowerCase(c) || Character.isDigit(c) || c == '-'
                || c == '_') {
            sb.append(c);
        }
    }
    String elementName = sb.toString();
    if (elementName.length() == 0 || (!Character.isUpperCase(elementName.charAt(0))
            && !Character.isLowerCase(elementName.charAt(0)))) {
        return "e" + elementName;
    }
    return elementName;
}

From source file:Main.java

public static boolean isDigit(String str) {
    char[] c = str.toCharArray();
    if (str.equals(""))
        return false;
    for (int i = 0; i < c.length; i++) {
        if (!Character.isDigit(c[i]))
            return false;
    }//w w w  .  j  a  v  a  2  s  .  c  o  m
    return true;
}

From source file:Main.java

/**
 * Starts reading the encoding from the first valid character until an
 * invalid encoding character occurs.//www. ja v  a  2 s. c o  m
 */
public static String encodingCleanup(String str) {
    StringBuilder sb = new StringBuilder();
    boolean startedWithCorrectString = false;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (Character.isDigit(c) || Character.isLetter(c) || c == '-' || c == '_') {
            startedWithCorrectString = true;
            sb.append(c);
            continue;
        }

        if (startedWithCorrectString)
            break;
    }
    return sb.toString().trim();
}

From source file:Main.java

public static String filterNumber(String number) {
    if (number == null)
        return null;

    int length = number.length();
    StringBuilder builder = new StringBuilder(length);

    for (int i = 0; i < length; i++) {
        char character = number.charAt(i);

        if (Character.isDigit(character) || character == '+')
            builder.append(character);//from w ww . j a v a2 s  .c o m
    }

    return builder.toString();
}

From source file:Main.java

/**
 * Converts any numbers and punctuation into standard ASCII
 * @param inputString/*from w w  w .  j a v  a  2s .co  m*/
 * @return
 */
static public String normalizeNumbersAndPunctuation(String inputString) {
    char[] chars = inputString.toCharArray();

    for (int curCharNum = 0; curCharNum < chars.length; curCharNum++) {
        char curChar = chars[curCharNum];
        if (Character.isDigit(curChar)) {
            int curDigit = Integer.parseInt(inputString.substring(curCharNum, curCharNum + 1));
            chars[curCharNum] = Integer.toString(curDigit).charAt(0);
        }
        if (Character.getType(curChar) == Character.DASH_PUNCTUATION || curChar == 8722)
            chars[curCharNum] = '-';
    }
    String returnString = new String(chars);
    return returnString;
}

From source file:Main.java

/**
 * Starts reading the encoding from the first valid character until an
 * invalid encoding character occurs./* w w  w  .  j a  va  2s.  co m*/
 */
public static String encodingCleanup(String str) {
    StringBuilder sb = new StringBuilder(str.length());
    boolean startedWithCorrectString = false;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        if (Character.isDigit(c) || Character.isLetter(c) || c == '-' || c == '_') {
            startedWithCorrectString = true;
            sb.append(c);
            continue;
        }

        if (startedWithCorrectString)
            break;
    }
    return sb.toString().trim();
}

From source file:com.hs.mail.imap.schedule.ScheduleUtils.java

public static Date getDateBefore(String str) {
    if (str != null) {
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            char ch = str.charAt(i);
            if (!Character.isDigit(ch)) {
                try {
                    int amount = Integer.parseInt(str.substring(0, i));
                    switch (Character.toUpperCase(ch)) {
                    case 'D':
                        return DateUtils.addDays(new Date(), -amount);
                    case 'M':
                        return DateUtils.addMonths(new Date(), -amount);
                    case 'Y':
                        return DateUtils.addYears(new Date(), -amount);
                    }/*  www.j  a  v  a  2 s .  c o m*/
                } catch (NumberFormatException e) {
                    break;
                }
            }
        }
    }
    return null;
}