Question
Write a method with the following header to convert a binary string to a decimal number. public static int bin2Dec(String binaryString) throws NumberFormatException Implement the
Write a method with the following header to convert a binary string to a decimal number.
public static int bin2Dec(String binaryString) throws NumberFormatException
Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string.
In the main method, ask the user to enter a binary string by using the next() method, call the method above, and display the correspond decimal value. Your program must catch the NumberFormatException and if the input string is not a binary string, the program should display an appropriate error message. Two sample runs are given below:
Enter a binary string: 10011
The corresponding decimal number is 19
Enter a binary number: 12011
java.lang.NumberFormatException: Binary number format error
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started