Write two unchecked exceptions, BinaryFormatException and HexFormatException, both extending NumberFormatException. Each receives the faulty text and the position of the bad character, and builds a message such as
"10201" is not binary: character '2' at position 2
Then write BaseConverter with the static methods binToDec(String) and hexToDec(String) (lower-case hex digits are accepted), which convert digit by digit and throw the matching exception at the first illegal character.
main reads pairs bin text or hex text until the end of the input; for any other first word it throws an IllegalArgumentException("Unknown base: …") itself. All errors are caught and counted. For the input
bin 1011
hex FF
bin 10201
hex 1G
oct 17
hex cafe
bin 0
the program prints
1011 in binary is 11
FF in hexadecimal is 255
Format error: "10201" is not binary: character '2' at position 2
Format error: "1G" is not hexadecimal: character 'G' at position 1
Unknown base: oct
cafe in hexadecimal is 51966
0 in binary is 0
4 converted, 3 error(s)