Exercise 12.6 implements the hex2Dec method to throw a NumberFormatException if the string is not a hex
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 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
Transcribed Image Text:
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 <= 'F') return 10 + ch - 'A'; else // ch is '0', '1', ..., return ch - '0'; hex char to decimal check uppercase or '9' 31 33
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 33% (6 reviews)
Output 210 Exception in thread main HexFormatException Illegal hex chara...View the full answer
Answered By
Somshukla Chakraborty
I have a teaching experience of more than 4 years by now in diverse subjects like History,Geography,Political Science,Sociology,Business Enterprise,Economics,Environmental Management etc.I teach students from classes 9-12 and undergraduate students.I boards I handle are IB,IGCSE, state boards,ICSE, CBSE.I am passionate about teaching.Full satisfaction of the students is my main goal.
I have completed my graduation and master's in history from Jadavpur University Kolkata,India in 2012 and I have completed my B.Ed from the same University in 2013. I have taught in a reputed school of Kolkata (subjects-History,Geography,Civics,Political Science) from 2014-2016.I worked as a guest lecturer of history in a college of Kolkata for 2 years teaching students of 1st ,2nd and 3rd year. I taught Ancient and Modern Indian history there.I have taught in another school in Mohali,Punjab teaching students from classes 9-12.Presently I am working as an online tutor with concept tutors,Bangalore,India(Carve Niche Pvt.Ltd.) for the last 1year and also have been appointed as an online history tutor by Course Hero(California,U.S) and Vidyalai.com(Chennai,India).
4.00+
2+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Listing 6.8 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...
-
Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string.
-
The Double.parseDouble() method requires a String argument, but it fails if the String cannot be converted to a floating-point number. Write an application in which you try accepting a double input...
-
Consider the frequency demodulation scheme in Figure in which the incoming FM signal s (t) is passed through a delay line that produces a phase-shift of ?/2 radians at the carrier frequency ? c . The...
-
Construct a random 4 ( 4 matrix A with integer entries between - 9 and 9, and compare det A with det AT, det (-A), det(2A), and det(10 A). Repeat with two other random 4 ( 4 integer matrices, and...
-
What is cost-volume-profit analysis and when is it particularly useful? (p. 76)
-
Briefly explain the following statement: Although long-term bonds are heavily exposed to interest rate risk, short-term T-bills are heavily exposed to reinvestment rate risk. The maturity risk...
-
Kroger Co.??s 2009 financial statements contained the following data (in millions). InstructionsCompute these values:(a) Working capital. (b) Currentratio. Current assets Total assets Current...
-
John and Sandy Ferguson got married eight years ago and have a seven-year-old daughter, Samantha. In 2021, John worked as a computer technician at a local university earning a salary of $154,100, and...
-
What is the disadvantage of having too many features in a language?
-
Exercise 12.7 implements the bin2Dec method to throw a BinaryFormatException if the string is not a binary string. Define a custom exception called BinaryFormatException. Implement the bin2Dec method...
-
Programming Exercise 11.1 defined the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the...
-
Peden's Advertising is building a rectangular sign with an area of 2 yd2 and a perimeter of 6 yd. Find the dimensions of the sign.
-
Solve for x . log 1 0 ( 4 x ) log 1 0 ( x 3 ) = 1
-
Let f ( x ) = ( 8 x - 4 x ^ 2 ) It is ^ x . Find the inflection points
-
f ( x ) = sin ( x ) / ( 2 * x ^ 2 + 4 ) , differentiate using quotient with respect to x
-
On a dreary morning in May 1995, Paul found himself sitting on the floor of the hallway, crouched against the cold wall, feeling dejected and desperate. The bustling cacophony of the people in nearby...
-
Two speakers S1 and S2 are at a distance from each other. Point Q is located at y = 2.1 m above loudspeaker S2 while point P is located at x = 4.1 m in front of loudspeaker S1. The two loudspeakers...
-
Write the C++ code for a void function that receives three double variables: the first two by value and the last one by reference. Name the formal parameters n1, n2, and answer. The function should...
-
How can NAFTA be beneficial to suppliers of Walmart?
-
Loris Manufacturing makes 1,000 products per month and has been increasing production at a rate of 6 percent each month. Write a program that accepts a production goal from the user. If the entered...
-
When you use recursion, you frequently ask a method to call itself repeatedly, _________________. a. Increasing the complexity of the problem with each call b. Using a simpler version of the problem...
-
Recursion can be successfully used to _________________. a. Solve mathematical problems b. Create visual patterns c. Solve mathematical problems and to create visual patterns d. Neither solve...
-
XYZ Corp. applies manufacturing overhead costs to products at a budgeted indirect-cost rate of $65 per direct manufacturing labor-hour. A retail outlet has requested a bid on a special order of a...
-
What did you observe to be the major causes for the volatile week in stock trading this past week?
-
Analyze why there was underpricing or overpricing on listing price for Change Healthcare (CHNG)
Dualities Dialectics And Paradoxes In Organizational Life 1st Edition - ISBN: 0198827431 - Free Book
Study smarter with the SolutionInn App