Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions

Question

What lessons were learned?

Answered: 1 week ago