Write a program that inputs a string that represents a binar

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 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 Notes: 1. Use the Scanner class nextLine method to input the binary number as a String. 2. 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. 3. Suppose you named the input String sInput. Then your “for” loop header would look like: for (int i = 0; i <= sInput.length() - 1; i++) 4. Note that charAt() returns a character as type “char”. For comparison, remember that character literals are enclosed in single quotation marks, like this: \'1\'. 5. 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).

Solution

import javax.swing.JOptionPane;

import java.text.DecimalFormat;

import java.util.Scanner;

public class Stringbinarynumber

{

public static void main(String [] args )

{

Scanner scan = new Scanner (System.in);

String Sinput;

System.out.print(\"Enter an binary number.\");

Sinput = scan.nextLine();

int n = 0;

for( int i =0;i<= Sinput.length();i++);

{

char binarynumber = Sinput.charAt(i);

if(binarynumber != 1)

{

System.out.print(\"Error, That was not a valid entry.\");

}

else if(binarynumber== 0)

{

System.out.print(\"Error, That was not a valid entry.\");

}

}

}

}

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 t
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 t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site