Q:

Create a triangle out of stars using recursion

I first need to create a method called printStars that prints n stars in one line. For example, printStars(5) displays the line *****. Using the printStars method, write a method printTriangle that creates a triangle of stars. For example, printTriangle(5) should display:
*
**
(***
(****
(*****
In the second part you should modify your code so that it displays this:
(*****
(****
(***
**
*
Solution to the second part:
public static String printStars(int n) {
    if(n == 0) {
        return "";
    }
    return "*" + printStars(n - 1);
}
public static String printTriangle(int n) {
    if(n == 0) {
        return "";
    }
    return printStars(n) + "\n" + printTriangle(n - 1);
}
public static void main(String[] args) {
    System.out.println(printTriangle(7));
}

The reason I can not solve the first part is it requires you to use the printStars method, so I have no idea how I can recursion so that the number of stars increases each line.

A:

You were close, first recurse in printTriangle and then printStars. Also, to prevent an empty line test for n equal to one. And prefer System.lineSeparator() over \n. Like,
public static String printTriangle(int n) {
    if (n <= 0) {
        return "";
    } else if (n == 1) {
        return printStars(1);
    }
    return printTriangle(n - 1) + System.lineSeparator() + printStars(n);
}

