Java: parse int value from a char

To parse an integer value from a char in Java, you can use the Character.getNumericValue() method. This method returns the numeric value of the specified char, or -1 if the char is not a digit.

For example:

char c = '5';
int i = Character.getNumericValue(c);  // i is 5

You can also use the Integer.parseInt() method to parse an integer value from a char, like this:

char c = '5';
int i = Integer.parseInt(String.valueOf(c));  // i is 5

Note that the Character.getNumericValue() method returns the numeric value of the char, not the ASCII value. For example, the char 'A' has an ASCII value of 65, but the Character.getNumericValue('A') returns -1, because 'A' is not a digit.

If you want to parse an integer value from a char that represents an ASCII value, you can use the (int) casting operator, like this:

char c = 'A';
int i = (int) c;  // i is 65