I've written a java program that converts a string of bits to its equivalent non-negative integer but I do not know how to add an error message when a character other than "1" and "0" is displayed. I want the following message "Error -- The string should consist of 1s and 0s only." to print and the program to stop. Please help! Here is my code so far:
import java.util.Scanner;
class BinaryToDecimal {
public static void main(String args[]) {
Scanner input = new Scanner( System.in );
System.out.print("Enter a binary number: ");
String binaryString =input.nextLine();
System.out.println ("The original string you entered:" binaryString);
System.out.println ("The equivalent integer is: "+Integer.parseInt(binaryString,2));
}
}