I am trying to output a checker pattern into a two dimensional array. How do I assign two characters in order to do this? Note array is a square  Please help.

``````// Takes two symbols and places them in alternate cells of board
public static char[][] placeSymbols(char charY, char charZ) {
// Generate first symbol character
String input2;
Scanner inCharY = new Scanner(System.in);
System.out.println("Enter first character: ");
input2 = inCharY.nextLine();
charY = input2.charAt(0);
System.out.println(charY);
// Generate second symbol character
String input3;
Scanner inCharZ = new Scanner(System.in);
System.out.println("Enter second character: ");
input3 = inCharZ.nextLine();
charZ = input3.charAt(0);
System.out.println(charZ);
// Insert characters into array
char square[][];
square = new char[size][size];
for (int row = 0; row < size; row++) {
for (int col = 0; col < square[row].length; col++) {

square[row][col] = charY; // Here assigning one character fills all the places

System.out.print(" " + square[row][col]);
}
System.out.println(" ");
}
return square;
}``````

Thanks very much for any help,
Jems

## All 7 Replies

You can use a boolean to tell you which letter to use. Instead of line 23 test the boolean, assign the appropriate char, then toggle the boolean true -> false or false -> true so that next time thru the loop you will print the other char.
Or
Get a bit a paper and write down the value of (row+col) for each square. Notice how the result goes odd/even in alternating squares? It's easy to test for odd/even...

You can use a boolean to tell you which letter to use. Instead of line 23 test the boolean, assign the appropriate char, then toggle the boolean true -> false or false -> true so that next time thru the loop you will print the other char.
Or
Get a bit a paper and write down the value of (row+col) for each square. Notice how the result goes odd/even in alternating squares? It's easy to test for odd/even...

Great thanks... I got the point of what you are suggesting but when I try say for example testing for odd even I come up shot on how to add two dimensional array positions. I did what you suggested and found each position to be odd-even but my code seem to have no impact.

``````// Insert characters into array
char square[][];
square = new char[size][size];
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
int test;
test = square[row][col]+square[row][col]; // Does not appear to add; not sure how to add arrays
if (test % 2 == 0) {
square[row][col] = charY; // add character at even array position
}
else {
square[row][col] = charZ; // add character at odd array position
}
System.out.print(" " + square[row][col]);
//System.out.print(" " + test); // checking to see what is being added but all zeros
}
System.out.println(" ");``````

On line 7 you are trying to add the contents of your 2D array (which is empty), when JamesCherrill suggested you just add row+col. Try changing line 7 to store the sum row+col in your test variable.

On line 7 you are trying to add the contents of your 2D array (which is empty), when JamesCherrill suggested you just add row+col. Try changing line 7 to store the sum row+col in your test variable.

Thanks JamesCherrill and Kramered...I am new at this and while I truly enjoy this course, I tend to frustrate myself by taking things too literal and over thinking my efforts.

I simply changed my code line to test = row + col which did the trick.

Thanks again, problem solved.

You can use a boolean to tell you which letter to use. Instead of line 23 test the boolean, assign the appropriate char, then toggle the boolean true -> false or false -> true so that next time thru the loop you will print the other char.
Or
Get a bit a paper and write down the value of (row+col) for each square. Notice how the result goes odd/even in alternating squares? It's easy to test for odd/even...

Used the odd/even approach which worked very well. The boolean was a little bigger challenge for me being a newbie. Can you please share a sample of such code lines please.

The code I used is below...

``````// Insert characters into array
char square[][];
int test; // Testing variable
square = new char[size][size];
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
test = row + col; // adds each position...works great!!!
if (test % 2 == 0) {
square[row][col] = charY; // add character at even array position
}
else {
square[row][col] = charZ; // add character at odd array position
}
System.out.print(" " + square[row][col]); // Prints my alternate output
//System.out.print(" " + test); // Used to see results of odd/even addition
}
System.out.println(" ");
}``````

Thanks again for the suggestion.

Jems

``````boolean x = false;
...
if (x) {
// do thing 1
} else {
// do thing 2
} x = !x;``````
``````boolean x = false;
...
if (x) {
// do thing 1
} else {
// do thing 2
} x = !x;``````

Excellent! I'll add this to my arsenal of conditional methods.

Thanks,
Jems

Be a part of the DaniWeb community

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