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...
-
Which of the following is a common job responsibility for someone in the database industry? a. Network security b. Data security c. Application programming d. Web app development
-
Consider the following cash flow profile and assume MARR is 10 percent/year. a. What does Descartes' rule of signs tell us about the IRR(s) of this project? b. What does Norstrom's criterion tell us...
-
Terry Industries produces two electronic decoders, P and Q. Decoder P is more sophisticated and requires more programming and testing than does Decoder Q. Because of these product differences, the...
-
Discuss the market concentration, competitiveness, and performance of the Australian grocery industry compared to other western countries. Identify the key stakeholders in the food supply chain, and...
-
What is Company XYZ's intrinsic enterprise value under the High Case, using the WACC as the discount rate and assuming the terminal value is based on the perpetual growth rate assumption outlined on...
-
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...
-
Potomac Leasing Co. leased an automatic telephone system to Vitality Centers. Claudene Cato signed the lease as guarantor of payments. When the rental was not paid, Potomac Leasing brought suit...
-
"What is the current understanding of the intricate crosstalk between the gut microbiome and the host immune system, and how do these interactions influence both local and systemic immunity, with...
-
Determine k for which vector = (5,-1, k) is t to v = (k+2, -3,2)
-
Why might an heir be happier to be the death beneficiary of a $5,000,000 life insurance policy held by an Irrevocable Life Insurance Trust, instead of being named to receive shares of stock valued at...
-
What is the molarity of a 350 ml solution, containing 13.6g AgNO3? (MM of AgNO3 is 169.87 g/mol)
-
Write 4-5 Pages Explain your self-assessment in relation to the nurse leadership competencies. Be sure to identify your strengths and potential gaps in relation to these competencies. Describe at...
-
On January 1, 2017, MM Co. borrows $340,000 cash from a bank and in return signs an 8% installment note for five annual payments of $85,155 each, with the first payment due one year after the note is...
-
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...
-
Komoro Foods Inc. has decided to track its "On Time and In Full percentages as a critical metric to know whether the distribution center is doing its job well. 1) Are there any factors that can...
-
Find a website that is useful in Emergency Management and then tell why you think?
-
I used ATLAS.ti to do the word count. I also did the training from ATLAS.ti, where they demonstrated how to use word lists to cut out certain words and focus on meaningful data. The original word...
Study smarter with the SolutionInn App