Repository: TheAlgorithms/Java
File: src/main/java/com/thealgorithms/conversions/BinaryToOctal.java (Java)

Code before:
package com.thealgorithms.conversions;

/**
 * Converts any Binary number to an Octal Number
 *
 * @author Zachary Jones
 */
public final class BinaryToOctal {
    private static final int BITS_IN_GROUP = 3;
    private static final int BINARY_BASE = 2;
    private static final int DECIMAL_BASE = 10;

    private BinaryToOctal() {
    }

    /**
     * This method converts a binary number to an octal number.
     *
     * @param binary The binary number
     * @return The octal number
     */
    public static String convertBinaryToOctal(int binary) {
        if (binary == 0) {
            return "0";
        }

        StringBuilder octal = new StringBuilder();
        int currentBit;
        int bitValueMultiplier = 1;

        while (binary != 0) {
            int octalDigit = 0;
            for (int i = 0; i < BITS_IN_GROUP && binary != 0; i++) {
                currentBit = binary % DECIMAL_BASE;
                binary /= DECIMAL_BASE;
                octalDigit += currentBit * bitValueMultiplier;
                bitValueMultiplier *= BINARY_BASE;
            }
            octal.insert(0, octalDigit);
            bitValueMultiplier = 1; // Reset multiplier for the next group
        }

        return octal.toString();
    }
}


Review comment (suggestion):
Consider adding input validation in the convertBinaryToOctal method to ensure that the input is a valid binary number (composed only of 0 and 1). If the input is invalid, the method could throw an IllegalArgumentException or handle it gracefully by returning an appropriate error message.

`if (!String.valueOf(binary).matches("[01]+")) {
    throw new IllegalArgumentException("Input is not a valid binary number.");
}
`


Code after:
package com.thealgorithms.conversions;

public final class BinaryToOctal {
    private static final int BITS_PER_OCTAL_DIGIT = 3;
    private static final int BINARY_BASE = 2;
    private static final int DECIMAL_BASE = 10;

    private BinaryToOctal() {
    }

    /**
     * This method converts a binary number to an octal number.
     *
     * @param binary The binary number
     * @return The octal number
     * @throws IllegalArgumentException if the input is not a valid binary number
     */
    public static String convertBinaryToOctal(int binary) {
        if (binary == 0) {
            return "0";
        }

        if (!String.valueOf(binary).matches("[01]+")) {
            throw new IllegalArgumentException("Input is not a valid binary number.");
        }

        StringBuilder octal = new StringBuilder();
        int currentBit;
        int bitValueMultiplier = 1;

        while (binary != 0) {
            int octalDigit = 0;
            for (int i = 0; i < BITS_PER_OCTAL_DIGIT && binary != 0; i++) {
                currentBit = binary % DECIMAL_BASE;
                binary /= DECIMAL_BASE;
                octalDigit += currentBit * bitValueMultiplier;
                bitValueMultiplier *= BINARY_BASE;
            }
            octal.insert(0, octalDigit);
            bitValueMultiplier = 1; // Reset multiplier for the next group
        }

        return octal.toString();
    }
}