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

1 import java.util.Scanner; 3 public class Hex2Dec { /** Main method */

public static void main(String[] args) { // Create a Scanner Scanner input

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

1 Expert Approved Answer
Step: 1 Unlock

Output 210 Exception in thread main HexFormatException Illegal hex chara... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming Questions!