Throw Exception in this code
in the following code converts Binary String into Decimal number
i want to throw NumberFormatException if the string isnt binary
how can i do that ?
Code:
public class Exercise8_8 {
public static void main(String[] args) {
// Prompt the user to enter a string
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter a binary number string: ");
String s = input.nextLine();
System.out.println("The decimal value is " + parseBinary(s));
}
public static int parseBinary(String binaryString) {
int value = binaryString.charAt(0) - '0';
for (int i = 1; i < binaryString.length(); i++) {
value = value * 2 + binaryString.charAt(i) - '0';
}
return value;
}
}
Thanks...