Exceptions • When the string parameter digits contains a character that does not represent a valid digit in the provided
Posted: Fri Jul 08, 2022 6:36 am
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; }