Question

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

JAVA:

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package org.student;

import java.util.Scanner;

public class ValidateBinaryNumber {

   public static void main(String[] args) {
       //Declaring variables
       String binary;
       int countone,count;
      
       //Scanner class object is used to read the inputs entered by the user
       Scanner sc=new Scanner(System.in);
  
       //This while loop continue to execute until the user enters other than 'y' or 'Y'
   while(true)
   {
       //Assigning zero to variables
       count=0;
       countone=0;
         
       //Getting the String entered by the user
           System.out.print("Enter Binary Number :");
           binary=sc.nextLine();
          
           /* Checking whether the entered string is valid binary number or not
           * if yes,count no of ones
           * if not,Display error message
           */
           for(int i=0;i<binary.length();i++)
           {
              
               if(binary.charAt(i)=='0' || binary.charAt(i)=='1')
               {
                   if(binary.charAt(i)=='1')
                   countone++;
               }
               else
               {
                   //Displaying error message
                   System.out.println("** Invalid.Binary Number Must contain only 0's and 1's **");
                   count++;
               }  
              
              
           }
          
           if(count==0)
           {
               //If the number of ones is 2 then execute if block
               if(countone==2)
               {
                   System.out.println("Accepted");
               }
               else
               {
                   System.out.println("Rejected");
              
               }  
           }      
          
           //Getting the character from the user 'Y' or 'y' or 'N' or 'n'
           System.out.print("Do you want to enter again(Y/N) ::");
           char ch = sc.next(".").charAt(0);
           if(ch=='Y'||ch=='y')
           {
               sc.nextLine();
           continue;
           }
           else
           {
           System.out.println(":: Program Exit ::");
           break;
           }
   }
       }

   }

___________________

Output:

Enter Binary Number :1A00
** Invalid.Binary Number Must contain only 0's and 1's **
Do you want to enter again(Y/N) ::y
Enter Binary Number :101000
Accepted
Do you want to enter again(Y/N) ::n
:: Program Exit ::

_________Thank You

Add a comment
Know the answer?
Add Answer to:
JAVA: Write a program that inputs a string that represents a binary number. The string can...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How would I write code in Java, where the user inputs a binary number and the...

    How would I write code in Java, where the user inputs a binary number and the system reads the input and outputs if there was an even or odd number of 1s typed. For example, if I typed 10101 it should output as odd. and if I typed 11011 it should output as even.

  • Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the...

    Write a C++ program that simulate a menu based binary number calculator.You don't have to write the main part of the program just the two functions for my C++ program. I wanted to post the sample code from my class but it won't allow me it's too long. This calculate shall have the following five functionalities: Provide sign extension for a binary number Provide two’s complement for a binary nunber string signed_extension(string b);              // precondition: s is a string that...

  • Write a program that inputs a string from the user representing a password, and checks its...

    Write a program that inputs a string from the user representing a password, and checks its validity. A valid password must contain: -At least 1 lowercase letter (between 'a' and 'z') and 1 uppercase letter (between 'A' and 'Z'). -At least 1 digit (between '0' and '9' and not between 0 and 9). At least 1 special character (hint: use an else to count all the characters that are not letters or digits). -A minimum length 6 characters. -No spaces...

  • Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three...

    Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities: Covert a binary string to corresponding positive integers Convert a positive integer to its binary representation Add two binary numbers, both numbers are represented as a string of 0s and 1s To reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the...

  • In java write a command-line program that helps to decrypt a message that has been encrypted...

    In java write a command-line program that helps to decrypt a message that has been encrypted using a Caesar cipher1. Using this method, a string may contain letters, numbers, and other ASCII characters, but only the letters (upper- and lower-case) are encrypted – a constant number, the shift, is added to the ASCII value of each letter and when letters are shifted beyond ‘z’ or ‘Z’ they are wrapped around (e.g. “Crazy?” becomes “Etcba?” when shifted by 2). When your...

  • Implement a Java method named addBinary() that takes two String arguments (each representing a binary value)...

    Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length. Note: ped() method is public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a =...

  • In C:Write a program that can determine whether a user input is a binary number or...

    In C:Write a program that can determine whether a user input is a binary number or not. (1, 2, 3, 8, 13) Write a program that accomplishes all of the following: Greets the user and informs them that the program will determine whether an input from the user is a valid binary number or not (e.g., consisting of only 0’s and/or 1’s). Accept an integer input from the user. Assume that the user provides a valid numeric input that will...

  • Write a method in java named isValidEmail that takes a string as input parameter, and returns...

    Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where:  user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter  domain represents a sequence of alphanumeric characters (i.e., letters...

  • Write a function called "phoneNumberFormat" that takes a string containing a ten-digit phone number (such as...

    Write a function called "phoneNumberFormat" that takes a string containing a ten-digit phone number (such as 5155551212) as input, convert into a more readable string with parentheses and dashes like (515)555-1212 and display it. If the input string contains more than or less than ten characters, display an error message. Note: Steps of phoneNumberFormat function can be given as follows: def phoneNumberFormat (phoneNum): 1. Let l be the length of the phoneNum 2. If 1 does not equal 10 then,...

  • Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs...

    Java Programming Use a one-dimensional array to solve the following program. Write an application that inputs six numbers [Keep the numbers in an array]. The numbers must be between 20 and 200, inclusive. If the number is not between 20 and 200, ask for another input. When a number is entered, display the number only if it is not a duplicate of a number already entered. If it is a duplicate of a number already entered, display a message that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT