Question

In: Computer Science

Write a program that inputs a string that represents a binary number. The string can contain...

Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

Examples of invalid binary numbers:
abc
10102011
10101FF
0000 1111 (note: contains a space)

Examples of valid, rejected binary numbers:
00000000
1111
01110000001

Examples of valid, accepted binary numbers:
1000001
1100

Your program will only test one input for each run of the program. Testing each of the test cases requires a separate run of your program for each one.

The following 3 test runs illustrate the format of the program's console input and output:

Enter a binary number > abc
Invalid binary number.

Enter a binary number > 01110000001
Rejected

Enter a binary number > 1000001
Accepted

Make sure you study the Final Project Guidance document.

Course Project Guidance
=======================

Getting the input
-----------------
Use the Scanner class nextLine method to input the binary number as a String.

Use the String charAt() method in a "for" loop that varies the index of the
character being examined. If your loop control variable is "i", then you will
use charAt(i) in the loop body to be able to examine the character at position "i".
As the loop progresses, you are able to get each character one-by-one. The
characters in a String are indexed from 0 to 1 less than the number of characters
in the String. The number of characters in the String can be determined by using
the String length() method.

Suppose you named the input String sInput. Then your "for" loop header would look like:
  for (int i = 0; i <= sInput.length() - 1; i++)

Note that charAt() returns a character as type "char". For comparison, remember that
character literals are enclosed in single quotation marks, like this: '1'.

Error message
-------------
When an invalid string is detected, display an error message, and then use a "return"
statement to exit from the main method (ending the program).

Purpose of "for" loop
---------------------
You must think about and understand the purpose of your "for" loop.
Don't try to fit your entire program into it.

For example, the "for" loop could count the number of invalid characters
(anything other than a '0' or a '1') and also it could count the number of
'1's it found.

Then, after the "for" loop is finished, that is after its closing brace, you
could have other code with "if" and/or "else" statements to decide what to output.

Of course, that means any variables you use in your post-"for" loop code must
be declared prior to the start of the "for" loop so they will still be in scope
after it finishes.

Solutions

Expert Solution

import java.util.Scanner;

public class ValidBinaryNumber {

   public static void main(String[] args) {

       // TODO Auto-generated method stub

      

       Scanner in = new Scanner(System.in);

       System.out.print("Enter a binary number >");

       String sInput = in.nextLine();

       boolean flag = false;

       int count = 0;

       for (int i = 0; i <= sInput.length() - 1; i++){

           if(sInput.charAt(i) == '0' || sInput.charAt(i) == '1' ){

               if(sInput.charAt(i) == '1')

                   ++count;

                 

           }else{

               System.out.println("Invalid binary number.");

               flag= true;

               break;

           }

             

       }

       if(!flag){

           if(count==2)

               System.out.println("Accepted");

           else

               System.out.println("Rejected");

          

       }

   }

}
======================================================================================

See The Output




Thanks, let me know if there is any concern.



See The Output



Related Solutions

1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the...
(Tokenizing Telephone Numbers) Write a program that inputs a telephone number as a string in the form (555) 555-5555. The program should use function strtok to extract the area code as a token, the first three digits of the telephone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The program should convert the area-code string to int and convert...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form...
Write a function called tokenizeTelNum that inputs a telephone number as a string in the form (555) 555-5555. The function should use the function strok to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. The function should convert the area code string to int and...
JAVA 1. Write an application that inputs a telephone number as a string in the form...
JAVA 1. Write an application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be printed....
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Write a recursive method to determine if a String is a palindrome. The program should contain...
Write a recursive method to determine if a String is a palindrome. The program should contain a String array that you can load several test cases to test your palindrome testing method. The program should load your String Array with the test data containing the possible palindromes from a text file. The program should test you application with a text file contained in your project folder After testing your program with your test cases in the text file, you program...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the...
Tokenizing Telephone Numbers Write java application that inputs a telephone number as a string in the form (555) 555-5555. The application should use String method split to extract the area code as a token, the first three digits of the phone number as a token and the last four digits of the phone number as a token. The seven digits of the phone number should be concatenated into one string. Both the area code and the phone number should be...
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT