Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a Java increasing alphabet grid with a 5×5 array

The grid I need to recreate looks like this

 ABCDE
T.....F
S.....G
R.....H
Q.....I
P.....J
 ONMLK

The grid I have now looks like this

 ABCDE
0.....0
1.....1
2.....2
3.....3
4.....4
 ONMLK

My code for creating the grid

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

// * Method * Creating the maze grid
public static void createMazeGrid(){
    // First section of Maze Grid
    System.out.print("  ");
    for(char alphabet = 'A'; alphabet < 'F'; alphabet++)
        System.out.print(alphabet);
    System.out.println();

    // Middle section of Maze Grid
    // *TRY TO FIGURE OUT ALPHABET GRID*
    for(int i = 0; i < maze.length; i++) {
        System.out.print(" ");
        for (int j = 0; j < maze[i].length; j++) {
            maze[i][j] = ".";
            if (j == 0)
                System.out.print(i + maze[i][j]);
            else if (j == maze[i].length - 1)
                System.out.print(maze[i][j] + i);
            else
                System.out.print(maze[i][j]);
        }
        System.out.println();
    }

    // Last section of Maze Grid
    System.out.print("  ");
    for(char alphabet = 'O'; alphabet > 'J'; alphabet--)
        System.out.print(alphabet);
    System.out.println();
}

I have already declared these variables in the public class, not my main method. How would I achieve this? I tried changing the int’s in the middle section of my map to char like my top and bottom section but it just cuts the middle portion of the map out.


public static int numRows = 5;


public static int numCols = 5;


public static String[][] maze = new String[numRows][numCols];

>Solution :

A simple way to complete your code is the use of chars where you already have your output.

public class Maze {
    public static int numRows = 5;
    public static int numCols = 5;
    public static String[][] maze = new String[numRows][numCols];
// * Method * Creating the maze grid
public static void createMazeGrid(){
    // First section of Maze Grid
    System.out.print("  ");
    for(char alphabet = 'A'; alphabet < 'F'; alphabet++)
        System.out.print(alphabet);
    System.out.println();

    // Middle section of Maze Grid
    // *TRY TO FIGURE OUT ALPHABET GRID*
    for(int i = 0; i < maze.length; i++) {
        System.out.print(" ");
        for (int j = 0; j < maze[i].length; j++) {
            maze[i][j] = ".";
            if (j == 0)
                System.out.print((char)('T' - i) + maze[i][j]); // left side
            else if (j == maze[i].length - 1)
                System.out.print(maze[i][j] + (char)('F' + i)); // right side
            else
                System.out.print(maze[i][j]);
        }
        System.out.println();
    }

    // Last section of Maze Grid
    System.out.print("  ");
    for(char alphabet = 'O'; alphabet > 'J'; alphabet--)
        System.out.print(alphabet);
    System.out.println();
}
public static void main(String[] args) { createMazeGrid(); }
}

Now let’s test it:

$ java Maze.java
  ABCDE
 T.....F
 S.....G
 R.....H
 Q.....I
 P.....J
  ONMLK

Looks good. 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading