alright ive been banging my head against a wall for three hours and i give up does anyone have a clue? the what im supposed to do is write a method named lotsOfStars that will accept two integers: the first integer indicates
the number of lines to print, the second the number of asterisks per line. The method should
print a rectangular arrangement of asterisks.

such as 3,4
****
****
****

Dani AI

Generated

Summary: the method lotsOfStars(int lines, int stars) should print a rectangle with the given number of rows and a given number of asterisks per row. The likely cause of a broken layout in this thread was printing a newline for every star instead of once per row — the same issue that and hinted at, and which solved with a working nested-loop example.

A compact, modern approach (Java 11+) builds one row string and prints it lines times. This avoids per-character I/O overhead and makes the intent clearer:

public static void lotsOfStars(int lines, int stars) {
    if (lines <= 0 || stars <= 0) return;
    String row = "*".repeat(stars);
    for (int i = 0; i < lines; i++) {
        System.out.println(row);
    }
}

For older Java versions, construct the row once with a filled char array or a StringBuilder and reuse it:

public static void lotsOfStars(int lines, int stars) {
    if (lines <= 0 || stars <= 0) return;
    char[] arr = new char[stars];
    java.util.Arrays.fill(arr, '*');
    String row = new String(arr);
    for (int i = 0; i < lines; i++) {
        System.out.println(row);
    }
}

Notes and troubleshooting: validate inputs (nonpositive values), prefer building a row once for better performance, and use System.lineSeparator() if a platform-specific newline is required. The nested-loop posted by is fine for learning; the shown alternatives are simply shorter or faster for large outputs.

Recommended Answers

All 5 Replies

Hints:

for (the number of lines you are given to print){
-print a newline.
for (the number of asteriks per line){
-print one asterik. Do not print a newline.
}


You can do printing using System.out.println and System.out.print. The former will print a newline (same as hitting enter basically) and the latter will not print a newline.
}

Indeed there must be the problem in using System.out.println instead of System.out.print in the inner loop.

Indeed there must be the problem in using System.out.println instead of System.out.print in the inner loop.

WHAT??

Indeed there must be the problem in using System.out.println instead of System.out.print in the inner loop.

What went wrong? I tried to guess the mistake the programmer (icke2433) might be making.

public static void lotsOfStars(int lines, int stars) {
        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < stars; j++) {
                System.out.print("*");
            }
            System.out.print("\n");

        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.