1. Member
Join Date
Jun 2011
Posts
6
Rep Power
0

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?

Java 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");```
Last edited by Fubarable; 06-04-2011 at 11:53 PM. Reason: code tags added

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
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().

3. 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.

4. 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•