Java Hollow Star Pattern (Inverted V)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An inverted V-shaped hollow pattern prints only the outline of an upside-down V: a single apex on row 1, then two stars that drift farther apart on each later row.

Remember
Rule: star when i == j (left) or i == k (right); else space

    *    
   * *   
  *   *  
 *     * 
*       *     ← 5 rows (width 9)

Unlike the filled inverted pyramid in Program 6, most cells are spaces. This outline is also the upper half of the hollow diamond — flip the outer loop in Program 8 to get the matching upright V.

How to Solve It

Two ways to emit the same outline — start with if/else legs, then optionally shorten with a ternary.

MethodIdeaBest for
If/else legsLeft j and right k loops; star when indices matchLearning, interviews, exams
Ternary ? :Same bounds; one-line star-vs-space choiceShorter demos once conditions click

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to 1:
        print "*" if i == j else " "
    for k from 2 to rows:
        print "*" if i == k else " "
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Left legfor (j = rows; j >= 1; j--) + if (i == j)
Right legfor (k = 2; k <= rows; k++) + if (i == k)
Line width2 * rows - 1
End the rowSystem.out.println();
Ternary shortcutSystem.out.print(i == j ? "*" : " ");
Flip laterfor (i = rows; i >= 1; i--) → Program 8

Printing Stars vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach * and each space
System.out.printlnEnds the current lineAfter both inner loops

Live Preview

Change the height and the hollow inverted V updates instantly — including width and star count.

Whole numbers from 1 to 14. Each line is 2 * rows - 1 characters wide.

Live result 5 rows · 9 stars
    *    
   * *   
  *   *  
 *     * 
*       *

Worked Walkthrough — rows = 4

Trace where each star lands for every outer-loop value of i (line width = 7).

iLeft star (j)Right star (k)StarsPrinted row
1j == 1none (k starts at 2)1*
2j == 2k == 22* *
3j == 3k == 32* *
4j == 4k == 42* *

Row 1 is the only single-star line — that is why the right loop must not start at k = 1. Total stars: 1 + 2 + 2 + 2 = 7 = 2×4 - 1.

Java Programs

Three complete programs: classic if/else, Scanner input, and a ternary shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — left loop j = rows..1, right loop k = 2..rows, star when indices match.

Java
public class InvertedVHollow {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (i == j)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            for (int k = 2; k <= rows; k++) {
                if (i == k)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Set height. rows = 5 means five outline lines (width 9).

2. Outer loop picks the row. i runs from 1 (apex) to rows (widest gap).

3. Left leg. j counts from rows down to 1; print * only when i == j.

4. Right leg, then break. k runs from 2 to rows with the same match rule, then println.

When i = 1 only the left loop prints a star; when i = 5 stars land at both outer columns.

Example 2 — User Input Version

Read the height at runtime with Scanner. Prefer hasNextInt() in real apps (shown in the tip below).

Java
import java.util.Scanner;

public class InvertedVHollowInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (i == j)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            for (int k = 2; k <= rows; k++) {
                if (i == k)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

How It Works

1. Prompt and read. Ask for a row count, then store it with sc.nextInt().

2. Same left/right core. Only the source of rows changes — the leg logic matches Example 1.

3. Safer input tip. Non-numeric input throws InputMismatchException. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Example 3 — Ternary ? : Form

Keep both loops; compress the star-vs-space choice into one expression each.

Java
public class InvertedVHollowTernary {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--)
                System.out.print(i == j ? "*" : " ");
            for (int k = 2; k <= rows; k++)
                System.out.print(i == k ? "*" : " ");
            System.out.println();
        }
    }
}

How It Works

1. Same outer loop. Still walk i from 1 to rows.

2. Same bounds. Left j still counts down; right k still starts at 2.

3. Shorter print. i == j ? "*" : " " replaces the multi-line if/else — same decision, less code.

Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = 1

Duplicate apex

Starting the right loop at k = 1 prints two stars on row 1. Keep k = 2.

j ascending

Mirrored left leg

The left loop must count j from rows down to 1. Ascending j flips the left diagonal.

println inside

Broken outline

If println is inside either inner loop, each cell lands on its own line. Use print for cells; println only after both loops.

rows = 1

Single apex

Output is just * — right loop never runs. A good sanity check.

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Use hasNextInt()

nextInt() throws on letters — prefer sc.hasNextInt() and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
If/else legs (Examples 1–2)O(rows²)O(1)
Ternary form (Example 3)O(rows²)O(1)

About n rows × 2n - 1 characters printed per row — still quadratic in n. Total stars = 2n - 1 (one apex + two per later row).

Key Takeaways

  • Rule: print * only when i == j (left) or i == k (right).
  • Two legs: left j counts down; right k starts at 2.
  • Break the row: call println only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i, print a star only when the left or right index matches i — start the right loop at 2.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row, the left loop runs j from rows down to 1 and prints a star only when i equals j. The right loop runs k from 2 to rows and prints a star only when i equals k. Every other cell is a space.
Printing columns from high j to low j places the star for row i when i equals j. As i grows, that match moves leftward in the left block, forming the descending left leg.
On row 1 the left loop already prints the apex at j equals 1. Starting k at 1 would print a second star on that row. Starting at 2 avoids duplicating the tip.
System.out.print stays on the same line. System.out.println ends the current line. Stars and spaces use print; the row break uses println after both inner loops.
Each line has width 2 * rows - 1: left block length rows, right block length rows - 1.
Program 8 uses the same inner loops but counts the outer loop from rows down to 1, so the wide row prints first and the legs meet at a bottom vertex.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Check sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

This hollow inverted V is the upper half of the hollow diamond. Starting the right loop at k = 2 is deliberate: on row 1 the left loop already prints the apex, so k = 1 would duplicate that star.

Next: V-Shaped Hollow

Reverse the outer loop and print the matching upright V outline.

Program 8 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful