Java text patterns
Generating text pattern is a regular requirement for students learning Java. Here I've written a collection of functions for you to try out.
import java.util.Arrays;
/**
* @version 2022-Nov-15
* @author azmeer
* @see https://www.javatpoint.com/how-to-print-pattern-in-java
* @see https://www.geeksforgeeks.org/printing-triangle-pattern-in-java/
*/
public class Patterns {
public static void square(String p) {
System.out.println("\nSquare Pattern");
int len = p.length();
for (int y = 0; y < len; y++) {
for (int x = 0; x < len; x++) {
System.out.print(p.charAt(x));
}
System.out.println();
}
}
public static void leftTriangleAsc(String p) {
System.out.println("\nLeft Triangle Ascending Pattern");
int len = p.length();
for (int y = 1; y <= len; y++) {
System.out.println(p.substring(0, y));
}
}
public static void leftTriangleDesc(String p) {
System.out.println("\nLeft Triangle Descending Pattern");
int len = p.length();
for (int y = len; y >= 1; y--) {
System.out.println(p.substring(0, y));
}
}
public static void rightTriangleAsc(String p) {
System.out.println("\nRight Triangle Ascending Pattern");
int len = p.length();
for (int y = 1; y <= len; y++) {
char[] spaces = new char[len - y];
Arrays.fill(spaces, ' ');
System.out.println(new String(spaces) + p.substring(0, y));
}
}
public static void rightTriangleDesc(String p) {
System.out.println("\nRight Triangle Descending Pattern");
int len = p.length();
for (int y = len; y >= 1; y--) {
char[] spaces = new char[len - y];
Arrays.fill(spaces, ' ');
System.out.println(new String(spaces) + p.substring(0, y));
}
}
public static void pyramid(String p) {
System.out.println("\nPyramid Pattern");
int len = p.length();
int i, j, row = len;
for (int y = 0; y < row; y++) {
for (j = row - y; j > 1; j--) {
System.out.print(" ");
}
for (j = 0; j <= y; j++) {
System.out.print("* ");
}
// throws the cursor in a new line after printing each line
System.out.println();
}
}
public static void bottomPyramid(String p) {
System.out.println("\nBottom Pyramid Pattern");
int rows = 8;
for (int i = 0; i <= rows - 1; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int k = 0; k <= rows - 1 - i; k++) {
System.out.print("*" + " ");
}
System.out.println();
}
}
public static void diamond(String p) {
System.out.println("\nDiamond Pattern");
int row, i, j, space = 1;
row = 10;
space = row - 1;
for (j = 1; j <= row; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= row - 1; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space++;
for (i = 1; i <= 2 * (row - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
}
public static void main(String[] args) {
String data = new String();
data = "0123456789";
System.out.println("Printing various triangles as string patterns \n");
square(data);
leftTriangleAsc(data);
leftTriangleDesc(data);
rightTriangleAsc(data);
rightTriangleDesc(data);
pyramid(data);
bottomPyramid(data);
diamond(data);
}
}

Leave a Comment