Total noob to java and I have a homework where I need to make a custom chess game but I am even struggling with printing the gameboard which I am trying all the empty spaces to be with "X".
My current "code":
public class Chess {
public static void main(String[] args) {
String[][] board = new String[7][7];
fillBoard(board);
}
public static void fillBoard(String[][] board){
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = "X";
System.out.print(Arrays.deepToString(board));
}
System.out.println();
}
}
}
It prints out:
This
When I want it to print out:
Is this
Just print the value of the position directly.
public static void fillBoard(String[][] board){
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
board[i][j] = "X";
System.out.print(board[i][j]);
}
System.out.println();
}
}
After reading your comments, I realized that it's not just about printing the array that you are concerned about; rather, you are concerned about how you will be able to access it later. The solution to the problem of incorrect printing has already been provided in the comments i.e. replacing System.out.print(Arrays.deepToString(board)); with System.out.print("X");. I am explaining here the other part (i.e. how to access it later) and also some additional information that will help you in designing your application.
When you pass and change an array in some method, the array will be changed for the calling method too (check this to learn more about it). It means that when board[][] will be changed in fillBoard, you will get the changed board[][] in main.
According to Single-responsibility principle, the module/method, fillBoard should do only one thing, which is filling up the board. It should not also do printing (additional thing). You should write a different method for printing.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[][] board = new String[7][7];
fillBoard(board);
displayBoard(board);
}
static void fillBoard(String[][] board) {
for (String[] row : board) {
Arrays.fill(row, "X");
}
}
static void displayBoard(String[][] board) {
for (String[] row : board) {
for (String cell : row) {
System.out.print(cell);
}
System.out.println();
}
}
}
Output:
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
XXXXXXX
Additional note:
A 2-D array is an array of 1-D arrays and therefore there are many ways in which you can write the code given above e.g.
static void fillBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
Arrays.fill(board[i], "X");
}
}
or using enhanced for loop:
static void fillBoard(String[][] board) {
for (String[] row : board) {
Arrays.fill(row, "X");
}
}
or without using Arrays.fill:
static void fillBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = "X";
}
}
}
Similarly, the method, displayBoard can be written as:
static void displayBoard(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j]);
}
System.out.println();
}
}
Note that I have used board.length and board[i].length instead of 7 for the number of rows and 7 for the number of columns to make sure you don't have to change it every time you change the size of the array.
Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.
public static void main(String[] args)
{
int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};
findfirst1(testarray);
}
public static void findfirst1(int[][] array1)
{
int value = 0;
for(int i = 0; i < 6;i++)
{
for(int k = 0; k < 7;k++)
{
value = array1[i][k];
if(value == 1)
{
System.out.println(value);
array1[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".
Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.
public class ChainCodeClass {
public static void main(String[] args)
{
int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
{0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};
int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
{0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0}};
System.out.print("First Image");
print(array1);
findfirst1(array1);
print(array1);
System.out.print("Second Image");
print(array2);
outline8(array2);
}
public static void findfirst1(int[][] array)
{
int value = 0;
for(int i = 0; i < array.length; i++)
{
for(int k = 0; k < array.length; k++)
{
value = array[i][k];
if(value == 1)
{
System.out.print(value + " ");
array[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
System.out.print("\n");
for(int k = 0; k < array.length; k++)
{
System.out.print(array[i][k] + " ");
}
}
System.out.println();
}
}
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to:
Write a program to assign the integer values 1 through 25 to a 25
element integer array. Then, print the array as five separate lines
each containing five elements separated by commas. The last element
on each line should be followed by a newline instead of a comma. The
output of your program should appear exactly as follows:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18,19,20
21,22,23,24,25
Hints:
One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is
0, it is the 5th number.
You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same
line.
I have a little bit of code but I don't know where to go from here:
public class Program4
{
public static int[] array;
public static void main(String[] args);
{
int[] numbers = new int [25]
for(int i=0; i<25; i++)
array[i] = i + 1;}
public static void printArray()
{
for(int i=1; i<=25; i++);
{
System.out.print(array[i - 1]);
if (i % 5 == 0)
System.out.printIn();
}
}
I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this,
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static int[] array;
public static void main(String[] args)
{
array = new int[25];
for(int i=0; i<25; i++)
array[i] = i + 1;
printArray();
}
public static void printArray()
{
int i;
for(i=1; i<=25; i++){
if (i % 5 != 0)
System.out.print(array[i-1]+",");
else
System.out.println(array[i-1]);
}
}
}
public class Foo {
public static int[] nums;
public static void main(String[] args) {
nums = new int[25];
for (int i = 0; i < nums.length; i++) {
nums[i] = i + 1;
}
printArray(nums);
}
public static void printArray(int[] myArray) {
for (int i = 0; i < myArray.length; i++) {
System.out.print(String.valueOf(myArray[i]);
if (i % 5 == 0) {
System.out.println();
} else if (i % 5 != 4){
System.out.println(", ");
}
}
}
public class Test {
public static void main(String[] args) {
int[] array = new int [25];
for(int i=0; i<25; i++) {
array[i] = i + 1;
}
for (int i=1; i<=25; i++) {
System.out.print(array[i - 1]);
if (i % 5 == 0) {
System.out.println();
} else {
System.out.print(", ");
}
}
}
}
And try to learn Java syntax first of all.
Here is an enhanced version of your code.
public class Program4
{
public static int[] array = new int[25];//instantiate the array to its default values
public static void main(String[] args)
{
//calling the methods from main
addToArray();
printArray();
}
//add the numbers to the array
public static void addToArray(){
for(int i=0; i<25; i++)
array[i] = i + 1;
}
//print the numbers from the array
public static void printArray()
{
for(int i = 1; i <= 25; i++){
if(i % 5 == 0){
System.out.print(i);
System.out.println();
}
else{
System.out.print(i + ",");
}
}
}
}