I am writing a code to indicate open and closed elements in a 2d array. An open element is represented with .
and a closed element is represented with *
. The first column in the array must be completely open whereas all the other columns must be closed. The output should be:
.**
.**
.**
The array is also going to vary in size as the user will determine how big the array is. I have managed to code a 2D array grid as this is not hard, however, I am trying to now use an if-statement to make the first column all open. I placed the if-statement within the nested loop:
import java.util.Arrays;
import java.util.Scanner;
public class trial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
int i;
int j;
for(i=0; i<grid.length; i ) {
for(j=0; j<grid[i].length; j ) {
grid[i][j] ='.';
if(grid[j].length > grid[0].length) {
System.out.println("");
}else {
System.out.print('*');
}
StdOut.print(grid[i][j]);
}
StdOut.println();
}
}
}
This, however, is giving me an output that looks like this:
*.*.*.
*.*.*.
*.*.*.
*.*.*.
I have tried placing the if-statement outside of this nested for-loop but I then get an error with my variables as they have not been defined. Any assistance as to how to fix this or make it that I can get values for my i
and j
variables outside of the for-loop would be greatly appreciated.
CodePudding user response:
I slightly modify your code to make it fit the description.
i
and j
will vary when the loop proceeds, and should be accessed inside the loop only.
It seems that you want to change the content inside grid
, which can be accessed outside the loop after processing.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
int i;
int j;
for (i = 0; i < grid.length; i ) {
for (j = 0; j < grid[i].length; j ) {
grid[i][j] = '.';
if(j > 0) {
grid[i][j] = '*';
}
System.out.print(grid[i][j]);
}
System.out.println();
}
// you cannot access i/j outside the loop scope. instead, you can access the grid
System.out.println("Loaded array:");
Arrays.stream(grid).forEach(System.out::println);
}
CodePudding user response:
Just check if the current column is 0 or not Just a minor change will work here I am writing only the main loop below
for(i=0; i<grid.length; i ) {
for(j=0; j<grid[i].length; j ) {
if(j == 0){ //0th column make it dot(.)
grid[i][j] ='.';
}
else { //For other columns put asterisk(*)
grid[i][j] = '*';
}
}
}
CodePudding user response:
Here's a different way to think about 2D arrays. They are simply a 1D array where each element is itself another 1D array (a complete "row").
With that in mind, here's a different way to initialize and print the contents of the 2D array using a mixture of enhanced and indexed for loops:
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("What size square grid do you want? ");
int m = sc.nextInt();
char[][] grid = new char[m][m];
for(char[] row : grid) {
for(int col=0; col<row.length; col ) {
row[col] = (col == 0) ? '.' : '*';
}
}
for(char[] row : grid) {
for(char c : row) {
System.out.print(c);
}
System.out.println();
}
}
Output:
What size square grid do you want?
5
.****
.****
.****
.****
.****