Results 1 to 4 of 4

Thread: Help me please!

  1. #1
    mr2insane is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Help me please!

    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;
                    System.out.print("Enter your binary number: ");
                    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 10:53 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,261
    Rep Power
    25

    Default

    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
  •