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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!