- Exceptions When The String Parameter Digits Contains A Character That Does Not Represent A Valid Digit In The Provided 1 (167.06 KiB) Viewed 34 times
Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided
Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided base, the code in this method should fail. • Copy/paste (into the provided answer field!) and rewrite the code to throw a RuntimeException, with appropriate text, for an error message when the code encounters an illegal digit. • For example, if base is 7 and digits is "10238401", then the code should fail because 8 is not a valid base-7 digit. • Assume that (1) the base itself is between 2 and 10 and (2) every character in digits is between 'o' and '9' • Note: DON'T use System.out.println() (Keep in mind, also, that you will be switching between numerical vs. character data types. For example, do not confuse the number 7 with the character '7' -- and so forth.) Code: public double basexConvert (String digits, int base) { double result = 0.0; int exponent = digits.length() - 1; for (char digit : digits.toCharArray ( ) ) result += (digit - '0') * Math.pow (base, exponent--); return result; }