Here you can find the source of toIntValue(char ch, int defaultValue)
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
Parameter | Description |
---|---|
ch | the character to convert |
defaultValue | the default symbol to use if the character is not numeric |
public static int toIntValue(char ch, int defaultValue)
//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('©') = 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'; } }