Hello everyone, I am working on a program that takes an input from a user and then tests whether it is a binary number or not. If it does not contain only 0's and 1's it prints out not a valid binary number. The part I am confused about is the second part where if it contains at least three consecutive 1's the program prints "accepted" and if it does not it prints "rejected".
I don't think I can use charAt(i) to compare the three consecutive 1's, but I don't know what I can use to test if there are three consecutive 1's in the string.
Thank you for any help you can provide.
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.print("Enter a binary number: ");
String input = sc.next();
boolean binary = true;
boolean validBinary = true;
for (int i = 0; i < input.length(); i++)
if (input.charAt(i) != '0' && input.charAt(i) != '1')
{
binary = false;
System.out.println("Not a valid binary number.");
break;
}
if (binary == true)
{
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '111')
validBinary = true;
if (validBinary == true)
{
System.out.println("Accepted");
break;
}
else
{
System.out.println("Rejected");
}
}
}
}
}