Validate that the entered number meets these requirements

Assignment Help Computer Engineering
Reference no: EM132210230

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 (execution) of the program. Make sure you study the Guidance document provided below.

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.

Output

The following 3 test runs illustrate the program'se input and output: Enter a binary number > abc Invalid binary number. Enter a binary number > 01110000001 Rejected Enter a binary number > 1000001 Accepted Note: Each of these requires a separate run (execution) of the program.

Reference no: EM132210230

Questions Cloud

Present a logical thesis driven essay : Present a logical thesis driven essay which argues its case. Marks will be deducted if the essay indulges in personal responses and empty discussions.
Court-ordered desegregation plan involving busing : An affluent school district was subject to a court-ordered desegregation plan involving busing.
Write a program to implement bubble sort : Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms.
Write a program to implement the checksum method : Write a program to implement the checksum method. Calculating the checksum at the sender.
Validate that the entered number meets these requirements : 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.
Do erickson and sizemore have contract : Do Erickson and Sizemore have a contract? If so, why? If not, are there any other theories of recovery available to Sizemore?
Implement the three disk-arm scheduling algorithms : Write a program to implement the three disk-arm scheduling algorithms. Write a driver program that generates a sequence of cylinder numbers .
Which of the four traps facing established firms : Which of the four traps (listed below) facing established firms do you see as the most prevalent and why?
Write a program in java to count the occurrences of word : Write a program in Java to count the occurrences of each word in a large text file (like a book)

Reviews

Write a Review

Computer Engineering Questions & Answers

  The document must be complete and demonstrate that you can

this is an individual project. using the case study below develop a 3 - 5 page paper that is well organized and

  Giga point-of- presence is an ultra high-speed network

giga point-of- presence is an ultra high-speed network access to the internet. in a 400 words excluding your

  Displays the frequency table of the characters in the file

Displays the frequency table of the characters in the file, and displays the Huffman code for each character.

  Program to find grade of students on basis of test scores

Write down a C++ program which finds a better grade for a student based upon two test scores. For a numeric average x.

  Describe processing computer crime and incident scenes

Describe processing computer crime and incident scenes. Explain ways to determine the best acquisition method. Describe how to validate data acquisitions. Apply the rules for controlling digital evidence.

  Differences between the unix and window traceroute

Write down the differences between UNIX (or Linux) and Window Traceroute.

  Create an array of structures containing structure members

Create an array of structures containing the structure members as defined .The program needs to be able to handle data for up to 500 books.

  Design and draw a class hierarchy for the objects

Design and draw a class hierarchy for the above objects, based on the given properties. In your class hierarchy place variable names next to each class .

  Prepare a test plan for the test of the option code field

The valid option codes are four-character alphabetic codes and should be matched against a list of valid codes. Prepare a test plan for the test of the option code field during data entry.

  Display the integers from first to last

Write an application that stores 12 integers in an array. Display the integers from first to last, and then display the integers from last to first.

  Importance of effective interpersonal communication

COIT20249 Assessment Details. Describe the basic principles and importance of effective interpersonal communication, active listening and reading for meaning

  Define vulnerability and risk in terms of security

Define vulnerability, threat and risk in terms of security. Why do you think sometimes they are used synonymously?

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd