The problem is this: Write the code that will translate hexadecimal digits (A - F, accept lower and uppercase) to its decimal values.
The input contains an character. If it is the hexadecimal digit print its decimal value else print -1.
I have a solution but I don't properly understand this line ch = input.nextLine().charAt( 0 );
import java.util.Scanner;
public class JavaApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char ch;
int digit;
ch = input.nextLine().charAt( 0 );
if( ch >= '0' && ch <= '9') digit = ch - '0';
else
if( ch >= 'a' && ch <= 'f') digit = ch - 'a' +10;
else
if( ch >= 'A' && ch <= 'F') digit = ch - 'A' +10;
else digit = -1;
System.out.println( digit );
}
}
Thanks for help