Java Integer Create toIntValue(char ch, int defaultValue)

Here you can find the source of toIntValue(char ch, int defaultValue)

Description

Converts the character to the Integer it represents, throwing an exception if the character is not numeric.

This method coverts the char '1' to the int 1 and so on.

 CharUtils.toIntValue('3', -1)  = 3 CharUtils.toIntValue('A', -1)  = -1 

License

Open Source License

Parameter

Parameter Description
ch the character to convert
defaultValue the default symbol to use if the character is not numeric

Return

the int symbol of the character

Declaration

public static int toIntValue(char ch, int defaultValue) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from  ww w  . j  a va  2  s  . co  m
     * <p>Converts the character to the Integer it represents, throwing an
     * exception if the character is not numeric.</p>
     * 
     * <p>This method coverts the char '1' to the int 1 and so on.</p>
     *
     * <pre>
     *   CharUtils.toIntValue('3')  = 3
     *   CharUtils.toIntValue('A')  = IllegalArgumentException
     * </pre>
     *
     * @param ch  the character to convert
     * @return the int symbol of the character
     * @throws IllegalArgumentException if the character is not ASCII numeric
     */
    public static int toIntValue(char ch) {
        if (isNumeric(ch) == false)
            throw new IllegalArgumentException("The character " + ch
                    + " is not in the range '0' - '9'");
        return ch - 48;
    }

    /**
     * <p>Converts the character to the Integer it represents, throwing an
     * exception if the character is not numeric.</p>
     * 
     * <p>This method coverts the char '1' to the int 1 and so on.</p>
     *
     * <pre>
     *   CharUtils.toIntValue('3', -1)  = 3
     *   CharUtils.toIntValue('A', -1)  = -1
     * </pre>
     *
     * @param ch  the character to convert
     * @param defaultValue  the default symbol to use if the character is not numeric
     * @return the int symbol of the character
     */
    public static int toIntValue(char ch, int defaultValue) {
        if (isNumeric(ch) == false)
            return defaultValue;
        return ch - 48;
    }

    /**
     * <p>Converts the character to the Integer it represents, throwing an
     * exception if the character is not numeric.</p>
     * 
     * <p>This method coverts the char '1' to the int 1 and so on.</p>
     *
     * <pre>
     *   CharUtils.toIntValue(null) = IllegalArgumentException
     *   CharUtils.toIntValue('3')  = 3
     *   CharUtils.toIntValue('A')  = IllegalArgumentException
     * </pre>
     *
     * @param ch  the character to convert, not null
     * @return the int symbol of the character
     * @throws IllegalArgumentException if the Character is not ASCII numeric or is null
     */
    public static int toIntValue(Character ch) {
        if (ch == null)
            throw new IllegalArgumentException(
                    "The character must not be null");
        return toIntValue(ch.charValue());
    }

    /**
     * <p>Converts the character to the Integer it represents, throwing an
     * exception if the character is not numeric.</p>
     * 
     * <p>This method coverts the char '1' to the int 1 and so on.</p>
     *
     * <pre>
     *   CharUtils.toIntValue(null, -1) = -1
     *   CharUtils.toIntValue('3', -1)  = 3
     *   CharUtils.toIntValue('A', -1)  = -1
     * </pre>
     *
     * @param ch  the character to convert
     * @param defaultValue  the default symbol to use if the character is not numeric
     * @return the int symbol of the character
     */
    public static int toIntValue(Character ch, int defaultValue) {
        if (ch == null)
            return defaultValue;
        return toIntValue(ch.charValue(), defaultValue);
    }

    /**
     * <p>Checks whether the character is ASCII 7 bit numeric.</p>
     *
     * <pre>
     *   CharUtils.isAsciiNumeric('a')  = false
     *   CharUtils.isAsciiNumeric('A')  = false
     *   CharUtils.isAsciiNumeric('3')  = true
     *   CharUtils.isAsciiNumeric('-')  = false
     *   CharUtils.isAsciiNumeric('\n') = false
     *   CharUtils.isAsciiNumeric('&copy;') = false
     * </pre>
     * 
     * @param ch  the character to validate
     * @return true if between 48 and 57 inclusive
     */
    public static boolean isNumeric(char ch) {
        return ch >= '0' && ch <= '9';
    }
}

Related

  1. toIntStr(String floatStr)
  2. toIntString(Integer integer)
  3. toIntString(Object object)
  4. toIntUnsigned(short x)
  5. toIntValue(char ch)
  6. toIntValue(final char ch)
  7. toIntValue(final Object o)
  8. toIntValue(Object number)
  9. toIntValueString(byte[] ip)