Binary to Decimal Converter

Easy
30
1
78.5% Acceptance

In this lab, you'll write a method named binaryToDecimal in the Main class that converts a binary number (input as a string) to its decimal equivalent. The method should be robust, handling valid inputs correctly and throwing an IllegalArgumentException for invalid inputs.

Requirements

  • Method Name: The method must be named binaryToDecimal.
  • Input: Accepts one string argument representing a binary number (e.g., "1010").
  • Output: Returns the decimal equivalent of the input binary string.
  • Error Handling: If the input string contains any characters other than "1" and "0", throw an IllegalArgumentException.

Edge Cases

  • Valid Binary String: The method should correctly convert binary strings to decimal numbers.
  • Invalid Characters: The method should throw an IllegalArgumentException for strings containing characters other than '0' and '1'.
  • Empty String: Consider how your method should behave with an empty string input.

Examples

  1. binaryToDecimal("1010") should return 10.
  2. binaryToDecimal("111") should return 7.
  3. binaryToDecimal("1001") should return 9.