Question: Exercise 12.6 implements the hex2Dec method to throw a NumberFormatException if the string is not a hex string. Define a custom exception called HexFormatException. Implement
Exercise 12.6 implements the hex2Dec method to throw a NumberFormatException if the string is not a hex string. Define a custom exception called HexFormatException. Implement the hex2Dec method to throw a HexFormatException if the string is not a hex string.
Listing implements the hex2Dec(String hexString) method, which converts a hex string into a decimal number. Implement the hex2Dec method to throw a NumberFormatException if the string is not a hex string.
Listing

![public static void main(String[] args) { // Create a Scanner Scanner input](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2022/11/636a72e1342c1_609636a72e123d8c.jpg)
1 import java.util.Scanner; 3 public class Hex2Dec { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input - new Scanner(System.in); 4 // Prompt the user to enter a string System.out.print("Enter a hex number: "); String hex - input.nextline(); 10 11 12 13 input string System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal (hex.toUpperCase())); hex to decimal 14 15 16 17 18 19 20 21 public static int hexToDecimal (String hex) { int decimalValue = 0; for (int i = 0; i < hex.length(); i++) { char hexChar = hex.charAt(i); decimalValue = decimalValue 16 + hexCharToDecimal (hexChar); 22 23 return decimalValue; 26 27 28 29 30 public static int hexCharToDecimal(char ch) { if (ch >= 'A' && ch
Step by Step Solution
3.32 Rating (149 Votes )
There are 3 Steps involved in it
Output 210 Exception in thread main HexFormatException Illegal hex chara... View full answer
Get step-by-step solutions from verified subject matter experts
