• 06-04-2011, 10:33 PM
mr2insane
Below I have attached my program that I am building. This works with determining if the String inputted is a binary number or not a binary number. My next task is: If the number is a binary number, and has three consecutive 1's, the program will out put "Accepted".

So my question is how in the world do I check to see if there are three consecutive 1's in the String?

Code:

Scanner scan = new Scanner (System.in);
String input;
input = scan.next();
System.out.print("Your binary number is: " + input + "\n");

int index;
int length = input.length();
char test;
String binary = "";
String nonbin = "";
{
for(index = 0; index < length; index++)
{
test = input.charAt(index);

switch (test)
{
case '0':
case '1':
binary += input.charAt(index);
break;
default:
nonbin += input.charAt(index);
}
}
}
{
int chk1 = binary.length();
if (chk1 == length)
{
System.out.print("Yes " + input + " is a binary number  \n");
}
else
{
System.out.print("No " + input + " is not a binary number");

• 06-04-2011, 10:41 PM
pbrockway2
Quote:

how in the world do I check to see if there are three consecutive 1's in the String?
You could use the String method indexOf().
• 06-04-2011, 10:54 PM
Fubarable
Moderator edit: code tags added to aid readability of code. Original poster, you can read up about how to do this yourself by checking out my link below on code tags.
• 06-04-2011, 11:04 PM
Norm
Quote:

how in the world do I check to see if there are three consecutive 1's in the String
how would you do it if you were doing it manually?
Probably need a loop and a counter and an if test.