Calculating a specific entry in a Pascal’s triangle recursively Our task was to calculate the entry of a Pascal’s triangle with a given row and column recursively. The triangle was specified in a way that the tip of the triangle is column = 0 and row = 0. That said, column 0 has always the entry 1.

My concerns are that the way I initialize the triangle as an array and filling in the entries are not so super.

class EntryOfPascalsTriangle {
    public static void main(String[] args) {
        validateInput(args);

        int row = 0;
        row = parseInteger(args[0]);

        int column = 0;
        column = parseInteger(args[1]);

        // A Pascal’s triangle expands quadratic.
        // There are never entries at a position greater column length
        if (column &gt; row) {
            handleError("There can't be entries in a column that doesn't exist in a Pascal's triangle.");
        }

        // We know already that the first and last entries in a row are always 1
        if (column == 0 || column == row) {
            System.out.println(1);
            System.exit(0);
        }

        int pascalsTriangle[][] = initPascalsTriangle(row);

        // initPascalsTriangle() takes care of the cases `0` and `1` already …
        if (row &gt; 1) {
            // … so we can start to fill the rows starting from 2.
            pascalsTriangle = fillPascalRows(pascalsTriangle, 2);
        }

        System.out.println(pascalsTriangle[row][column]);
    }

    /**
     * Initializes a Pascal’s triangle with all cells filled with `0`
     * except for the first and last cell in a row which are `1`
     */
    private static int[][] initPascalsTriangle(int rows) {
        int[][] pascalsTriangle = new int[rows+1][];

        // Iterate over the columns of the rows of pascalsTriangle
        for (int cell = 0; cell &lt; pascalsTriangle.length; cell++) {
            pascalsTriangle[cell] = new int[cell+1];

            // Fill the first cell of each row with `1`
            pascalsTriangle[cell][0] = 1;

            // Also fill the last cells with `1`
            pascalsTriangle[cell][cell] = 1;
        }

        return pascalsTriangle;
    }

    /**
     * Fills in the cells of Pascal’s triangle recursively
     */
    private static int[][] fillPascalRows(int[][] pascalsTriangle, int row) {
        for (int cell = 1; cell &lt; pascalsTriangle[row].length-1; cell++) {
            pascalsTriangle[row][cell] = pascalsTriangle[row-1][cell-1] + pascalsTriangle[row-1][cell];
        }

        if (row &lt; pascalsTriangle.length-1) {
            fillPascalRows(pascalsTriangle, row+1);
        }

        return pascalsTriangle;
    }

    /**
     * We need to make sure that two arguments are provided
     * and fail with a helpful message if they weren’t.
     */
    private static void validateInput(String[] args) {
        if (args.length &lt; 2) {
            handleError("Not enough arguments.");
        }

        if (args.length &gt; 2) {
            handleError("Too many arguments.");
        }
    }

    /**
     * Small wrapper to parse strings as integers.
     */
    private static int parseInteger(String arg) {
        int result = 0;

        try {
            result = Integer.parseInt(arg);
        } catch (NumberFormatException nfe) {
            handleError("'" + arg + "' was not an integer.");
        }

        return result;
    }

    /**
     * Small wrapper to produce error messages
     */
    private static void handleError(String errorMessage) {
        String basicUsage = "Please enter the column and the row of a Pascal's triangle.";
        System.out.println(errorMessage);
        System.out.println("\n" + basicUsage);
        System.exit(1);
    }
}
Since this question is marked as beginner, let me compliment the neat structure and consistent style I see throughout. It's well done.

Variable, method, and class names are all meaningful, and useful.

The validation and error handling systems are uncommonly comprehensive, and again, well done.

Validation

The validation should provide more feedback to the user. Saying 'not enough arguments' is fine, but then you should also say how many are expected. Does the user have to engage on a guessing game, bouncing between too many arguments, and not enough arguments before discovering how many they need?

Pre-initializing variables

There is no need to preinitialize variables. The following:


    int row = 0;
    row = parseInteger(args[0]);

    int column = 0;
    column = parseInteger(args[1]);



can just be:

    int row = parseInteger(args[0]);
    int column = parseInteger(args[1]);


Algorithm

THis is where I see the biggest problem, and it's not because you've made a mess, but because I think you've missed the point. If the problem is: "... to calculate the entry of a Pascal’s triangle with a given row and column recursively" then that is not what you have done.

You have calculated all entries and stored them in an array, then indexed the values you need. Sure, some parts are recursive, but I expect that the point is to not have the arrays at all.

Consider the triangle:

1
1   1
1   2   1
1   3   3   1
1   4   6   4   1
.....


The 'simple' solution is to have a method like:

public int getPascalValue(int row, int column) {
    if (column == 0 || column == row) {
        return 1;
    }
    return getPascalValue(row - 1, column - 1) + getPascalValue(row - 1, column);
}


That is a system for recursively calculating the value at a given row/column (assuming valid input).

It does a fair number or recalculations, but the point is that it solves the problem as stated.