0

This is part of my code, I was instructed to write a program that accepts a binary number as a string, and that will only show "Accepted" if the total number of 1's is 2. There is more to it, but getting to the point where it is counting the 1's is my problem at the moment. If anyone could point me in the direction of my error, it would be most appreciated.

import java.util.Scanner;


public class BinaryNumber
{
  public static void main( String [] args )
  {

   Scanner scan = new Scanner(System.in);
   String input;
   int count = 0;

   System.out.print( "Enter a binary number > ");
   input = scan.nextLine( );


    for ( int i = 0; i <= input.length()-1; i++)
    {
     char c = input.charAt(i);

      if ((c == '1') && (c == '0'))
           if (c == '1')
              {count++;}
              if (count == 2)
                 {System.out.println( "Accepted" );
                 }
                if (count != 2)
                   {System.out.println( "Rejected" );
                    System.out.print( "Enter a binary number > ");
                    input = scan.nextLine( );
                   }
8
  • 6
    c == '1' && c == '0' will never be true Commented Nov 22, 2013 at 23:33
  • You really need to reformat this code. I have spent 3 minutes so far trying to figure out where the if statements begin and end. Commented Nov 22, 2013 at 23:36
  • My professor is the one that told me to put that condition...so there ya go...any suggestions? Also, I tried doing the count before the crazy condition, no luck ): Commented Nov 22, 2013 at 23:36
  • Maybe if they could both be true if you put your computer in a box with a bit of radioactive material and a Geiger counter.... Commented Nov 22, 2013 at 23:39
  • Maybe it is a typo for (c=='1') || (c=='0') Commented Nov 22, 2013 at 23:40

1 Answer 1

8

The problem is that if ((c == '1') && (c == '0')) will never be true.

You need to check if the character is 1 OR 0 and then check if it's a '1' to increment your counter.

int count;
Scanner scan = new Scanner(System.in);
String input;
boolean notValid = false; //to say if the number is valid
do {
      count = 0;
      System.out.print("Enter a binary number > ");
      input = scan.nextLine();
      for (int i = 0; i <= input.length()-1; i++){
         char c = input.charAt(i);
         if(c == '0' || c == '1'){
             if (c == '1'){
                 count++;
                 if(count > 2){
                   notValid = true;
                   break; //<-- break the for loop, because the condition for the number of '1' is not satisfied
                 }
             }
         }
         else {
             notValid = true; // <-- the character is not 0 or 1 so it's not a binary number
             break;
         }
      }
 }while(notValid);  //<-- while the condition is not reached, re-ask for user input
 System.out.println("Done : " + input);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.