import java.util.Scanner; public class Hex2Dec2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter a 2-digit hex number: "); String hex = in.nextLine().trim(); in.close(); if (hex.length() != 2) { System.err.println("Invalid input"); System.exit(1); } int value = 0; char ch = Character.toUpperCase(hex.charAt(1)); if ('A' <= ch && ch <= 'F') value += ch - 'A' + 10; else if (Character.isDigit(ch)) value += ch - '0'; else { System.err.println("Invalid digit"); System.exit(2); } ch = Character.toUpperCase(hex.charAt(0)); if ('A' <= ch && ch <= 'F') value += (ch - 'A' + 10) * 16; else if (Character.isDigit(ch)) value += (ch - '0') * 16; else { System.err.println("Invalid digit"); System.exit(2); } System.out.printf("The value of %s is %d", hex, value); } }