MultipleLoop.java

 avatar
unknown
java
4 years ago
5.9 kB
6
Indexable
import java.util.Scanner;

public class MultipleLoop {
    /**
     * Helper method to identify if a given string is a binary string or not.
     * Note that a binary string should only contain 0s or 1s. If the string contains any other character, then it is not binary.
     *
     * @param binary the binary string to be validated.
     * @return true if the string is binary, else false.
     */
    public static boolean isValidBinary(String binary) {
        // throw exception if the string is null
        if (binary == null) {
            return false;
        }
        // loop through each character of the binary string
        for (char ch : binary.toCharArray()) {
            // return false if the current character is not 0 and not 1.
            if (ch != '0' && ch != '1') {
                return false;
            }
        }
        // the control will come here if the string contains only 0s or 1s, hence return true.
        return true;
    }

    /**
     * Helper method to count the number of 0s in a binary string.
     *
     * @param binary the binary string to be validated.
     * @return the number of 0s in the binary string.
     * @throws IllegalArgumentException if the string is not binary
     */
    public static int countZeroes(String binary) {
        // throw an exception if the string is not binary
        if (!isValidBinary(binary)) {
            throw new IllegalArgumentException("THe given string is not binary..");
        }
        int zeroesCount = 0; // stores the count of 0s in the binary string
        // loop through each character of the binary string
        for (char ch : binary.toCharArray()) {
            // increment zeroesCount by 1 if the current character is 0
            if (ch == '0') {
                zeroesCount++;
            }
        }
        // return zeroesCount
        return zeroesCount;
    }

    /**
     * Helper method to count the number of 1s in a binary string.
     *
     * @param binary the binary string to be validated.
     * @return the number of 1s in the binary string.
     * @throws IllegalArgumentException if the string is not binary
     */
    public static int countOnes(String binary) {
        // throw an exception if the string is not binary
        if (!isValidBinary(binary)) {
            throw new IllegalArgumentException("THe given string is not binary..");
        }
        int onesCount = 0; // stores the count of 1s in the binary string
        // loop through each character of the binary string
        for (char ch : binary.toCharArray()) {
            // increment zeroesCount by 1 if the current character is 1
            if (ch == '1') {
                onesCount++;
            }
        }
        // return onesCount
        return onesCount;
    }

    /**
     * Helper method to print the menu.
     */
    public static void menu() {
        System.out.print("1 - Get Binary Code\n" +
                "2 - Count Zeroes\n" +
                "3 - Count Ones\n" +
                "4 - Exit\n" +
                "Enter your choice: ");
    }

    // driver code to test
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // used for taking system input
        String binary = null; // stores the binary string inputted by user

        // loop infinitely until the user enters "exit"
        while (true) {
            // print menu
            menu();
            // get the user choice
            int choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    // the user has opted to enter a binary string
                    // reset the binary string back to null
                    binary = null;

                    // prompt the user to enter a binary number
                    System.out.print("Enter a binary number: ");
                    // store the user inputted value into variable 'binary'
                    binary = scanner.next();

                    // keep iterating until the user has entered a valid binary string.
                    while (!isValidBinary(binary)) {
                        // prompt the user again to enter a binary number
                        System.out.print("Not a valid binary number, enter a binary number again: ");
                        // store the user inputted value into variable 'binary'
                        binary = scanner.next();
                    }
                    break;
                case 2:
                    // the user has opted to count the number of 0s
                    try {
                        System.out.println("Number of zeroes = " + countZeroes(binary));
                    } catch (Exception e) {
                        // this catch block will execute if the user has not entered a valid binary string
                        System.out.println("Please enter a valid binary string first....");
                    }
                    break;
                case 3:
                    // the user has opted to count the number of 1s
                    try {
                        System.out.println("Number of ones = " + countOnes(binary));
                    } catch (Exception e) {
                        // this catch block will execute if the user has not entered a valid binary string
                        System.out.println("Please enter a valid binary string first....");
                    }
                    break;
                case 4:
                    // the user has opted to exit
                    System.out.println("Exiting. Good bye...");
                    System.exit(0);
                default:
                    // the user has entered an invalid choice
                    System.out.println("Invalid choice entered, try again!!");
            }
            System.out.println();
        }
    }
}
Editor is loading...