Printing 2D Array in Java - java

I'm trying print a 10x10 grid, I don't need any particular symbols, just the 10x10 format. There's actually a lot more to the whole program but right now I'm stuck on this. I really just want to print *'s for a simple Pacman game. I'm not good at programming at all, but I have to pass so I can graduate next semester. Here is what I have so far;
public class Pacman {
public static void main(String[] args) {
int columns = 0;
int rows = 0;
int[][] grid = new int[rows][columns];
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid.length; j++){
System.out.println(grid[i][j] + " ");
}
System.out.println();
}
}
}
I don't have errors in syntax or compiling, but nothing actually prints.

int columns = 10;
int rows = 10;
int[][] grid = new int[rows][columns];
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid.length; j++)
{
grid[i][j] = 0;
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
That will print out a 10x10 with every number in the array being 0

public static void main(String[] args) {
int columns = 10;
int rows = 10;
int[][] grid = new int[rows][columns];
for (int i = 0; i < grid.length; i++){
for (int j = 0; j < grid.length; j++){
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}

Related

How can I print a two-dimensional array of characters in Java, to create a 20x20 grid?

I am trying to print a 2d array of periods in Java, however, I can not get the formatting correctly. I am able to create a similar layout NOT using a 2d array. However, I will need to be working with a 2d array to finish the project. I have tried using Arrays.deepToString(); but did not find it useful.
My goal is to have a 20x20 grid of periods like this:
** Without the S and the X
My way without using a 2d array:
for (int i = 20; i >= 1; i--) {
for(int j = 1; j <= 20; j++) {
System.out.print(" .");
}
System.out.print("\n");
}
My try using a 2d array:
final int rows = 20;
final int columns = 20;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
board[i][j] = ".";
}
System.out.println(Arrays.deepToString(board));
}
Put the two together?...
public static void main(String[] args) {
final int rows = 20;
final int columns = 20;
String board[][] = new String[rows][columns];
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = ".";
}
}
display2Darray(board);
}
public static void display2Darray(String[][] arr) {
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
System.out.print(arr[row][col]);
}
System.out.println();
}
}
You have to print your 2D array outside outer loop but you print this 2D array result outside inner loop. After that you have to remove bracket and comma through java replace() method.
Here down is modified code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
final int rows = 20;
final int columns = 20;
String board[][] = new String[rows][columns];
// OUTER LOOP
for (int i = 0; i < rows; i++) {
// INNER LOOP
for (int j = 0; j < columns; j++) {
board[i][j] = ".";
}
}
System.out.print(Arrays.deepToString(board).replace("],", "\n")
.replace(",", "")
.replace("[[", " ")
.replace("[", "")
.replace("]]", ""));
}
}

why is system.out.println using 2-d arrays giveing error?

I am trying to populate my 2-D array with random intergers. The only issue I am having is the last system.out.println(); is highlighted in red. It says error can't resolve symbol 'println'
int[][] array = new int[row][col];
for (int i = 0; i <= rows; i++) {
System.out.println(i);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.println(j);
}
array[row][col] = ((int)(Math.random() * 10));
System.out.println(array[row][col]);
}
// this last line is highlighted in red
System.out.println();
}
}
You can try this
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt ();
System.out.println("column?");
col = sc.nextInt ();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
}
}
for(int i = 0 ;i < row ;i++) {
for(int j = 0 ; j < col ; j++) {
System.out.print(array[i][j] );
}
System.out.println();
}
}
}
You need to take care not to go beyond the end of the arrays, so the <= need to be <. You also want to use the for loops within each other to get the indices right. Thus:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.println("row " + i + " column " + j + " " + array[i][j]);
}
}
// this last line is highlighted in red
System.out.println();
}
}
REVISION: now with pretty print of table:
import java.util.*;
public class Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int row;
int col;
System.out.println("row?");
row = sc.nextInt();
System.out.println("column?");
col = sc.nextInt();
System.out.print("```|");
for (int j = 0; j < col; j++) {
System.out.print(" " + j);
}
System.out.println();
for (int j = 0; j <= col; j++) {
System.out.print("----");
}
System.out.println();
//if you want to print any header inforation or the values of row and column, do it here outside of the loops
int[][] array = new int[row][col];
for (int i = 0; i < row; i++) {
System.out.print(i + " |");
for (int j = 0; j < col; j++) {
array[i][j] = ((int)(Math.random() * 10));
System.out.print(" " + array[i][j]); //feel free to add vertical bars here if you wish
}
System.out.println();
}
}
}

How can I create a rectangle with two-dimensional Arrays?

In this exercise we have to draw a rectangle using two-dimensional arrays that goes from 10 to 15 rows and 20 to 30 column, putting in the border of the rectangle "#" while putting inside the rectangle "-". It has to look something like this: https://i.stack.imgur.com/IOqY6.png
The code I have so far is this, but I need some help fixing it since I'm a bit lost with the exercise:
public class Practica9{
public static void main(String[] args){
char [][] tablero = new char [10][20];
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
tablero [0][19] = #;
tablero [9][19] = #;
System.out.println (tablero[0][19]);
System.out.println (tablero[9][19]);
}
}
for (int i = 0; i < 10; i++){
for (int j = 0; j < 20; j++){
tablero [1][18] = -;
tablero [8][18] = -;
System.out.println (tablero [1][18]);
System.out.println (tablero [8][18]);
}
}
}
}
public static void main(String[] args){
int rows = 15;
int columns =30;
char [][] rectangle = new char[rows][columns];
// fill array
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
if(i==0 || j==0 || i==rows-1 || j==columns-1){
rectangle[i][j] = '#';
}
else{
rectangle[i][j] = '-';
}
}
}
// print array
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
System.out.print(rectangle[i][j]);
}
System.out.println();
}
}

java performing array calculation [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
The code below will print two square matrices and I need them to perform multiplication between the two matrices but i cant seem to get that part working. I put a comment right before that block of code where the problem is. But for now all it prints is zeros. Iv been looking online at a lot of sites but cant seem to get mine to work.
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
//create the grid
final int rowWidth = 9;
final int colHeight = 9;
Random rand = new Random();
int [][] board = new int [rowWidth][colHeight];
//fill the grid
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = rand.nextInt(10);
}
}
//display output
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();
}
System.out.println();
}
System.out.println();
int [][] board2 = new int [rowWidth][colHeight];
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board[row2].length; col2++) {
board[row2][col2] = rand.nextInt(10);
}
}
//display output
for(int m = 0; m < board2.length; m++) {
for(int n = 0; n < board[m].length; n++) {
System.out.print(board[m][n] + " ");
}
System.out.println();
}
//error is somewhere here
int[][] calculationMultiplication = new int[rowWidth][colHeight];
for (int l = 0; l < rowWidth; l++) {
for (int t = 0; t < colHeight; t++) {
for (int z = 0; z < rowWidth; z++) {
calculationMultiplication[l][t] = calculationMultiplication[l][t] + board[l][z] * board2[z][t];
}
}
}
//display output
System.out.println("\nProduct of the 2 matrices is ");
for (int i = 0; i < calculationMultiplication.length; i++) {
for (int j = 0; j < calculationMultiplication[0].length; j++) {
System.out.print(calculationMultiplication[i][j] + " ");
}
System.out.println();
}
} //end of main
} //end of class Main
The problem is that you've not filled the board2 array, so all its elements will be 0. In the for loop, you should also assign random values to board2. You are doing it twice for the board array.
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board2[row2].length; col2++) { // notice 'board2[row].length'
board2[row2][col2] = rand.nextInt(10);
}
}
You should do something similar in the for loops where you display the array:
//display output
for (int m = 0; m < board2.length; m++) {
for (int n = 0; n < board2[m].length; n++) { // notice 'board2[row].length'
System.out.print(board2[m][n] + " ");
}
System.out.println();
}
board2 is always zero.
You fill the wrong array here:
//fill the grid
for (int row2 = 0; row2 < board2.length; row2++) {
for (int col2 = 0; col2 < board[row2].length; col2++) {
board[row2][col2] = rand.nextInt(10);
}
}
Basically you just did simple copy paste mistakes that can be avoidable if you used methods. I'll tell you how but first here's how you can discover your mistake:
System.out.println(String.format("%d * %d = %d",board[l][z], board2[z][t], board[l][z] * board2[z][t]));
Add this just before doing the calculation for calculationMultiplication[l][t]. Try if you want...
Anyway getting back to your mistakes, you have populated your first matrix only, the second one contain zeros (verify your code in the for loop for inserting random numbers into the second matrix), therefore any number multiplied by zero is equal to zero.
I haven't looked at all your code, because it contain a lot of copy paste which confused you and could confuse any one as well, so here's a better way to avoid mistakes in printing the matrix and inserting random numbers:
Printing:
void printMatrix(int[][] matrix){
for(int row =0; row < numOfRows; row++){
for(int col =0; col < numOfCols; col++){
System.out.print(matrix[row][col]+" ");
}
System.out.println(); // new line
}
}
Inserting:
void insertMatrix(int[][] matrix){
for(int row =0; row < numOfRows; row++){
for(int col =0; col < numOfCols; col++){
matrix[row][col] = rand.nextInt(10); // rand must be declared outside any method
}
}
}
Putting all together:
import java.util.*;
public class Main {
// create the grid
final static int rowWidth = 9;
final static int colHeight = 9;
static Random rand;
public static void main(String[] args) {
rand = new Random();
int[][] board = new int[rowWidth][colHeight];
int[][] board2 = new int[rowWidth][colHeight];
int[][] calculationMultiplication = new int[rowWidth][colHeight];
// fill
insertMatrxi(board);
// display output
printMatrix(board);
System.out.println();
// fill
insertMatrxi(board2);
// display output
printMatrix(board2);
for (int l = 0; l < rowWidth; l++) {
for (int t = 0; t < colHeight; t++) {
for (int z = 0; z < rowWidth; z++) {
calculationMultiplication[l][t] += board[l][z] * board2[z][t];
}
}
}
// display output
System.out.println("\nProduct of the 2 matrices is ");
printMatrix(calculationMultiplication);
} // end of main
public static void printMatrix(int[][] matrix) {
for (int row = 0; row < rowWidth; row++) {
for (int col = 0; col < colHeight; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println(); // new line
}
}
public static void insertMatrxi(int[][] matrix) {
for (int row = 0; row < rowWidth; row++) {
for (int col = 0; col < colHeight; col++) {
matrix[row][col] = rand.nextInt(10);
}
}
}
} // end of class Main

2d ArrayList in Java adding data

I need little help on a homework assignment. I have to create a 10 by 10 ArrayList, not an array. This is what I have and I just need a hint on how to do a for loop to add the date to the 2D ArrayList. By the way this is for putting data that are grades; going from 100 to 82. (Yes I know it is homework but need to be pointed in the correct direction)
public void q6()
{
//part a
ArrayList<ArrayList<Double>> grades;
//part b
grades = new ArrayList<ArrayList<Double>>(10);
//second dimension
grades.add(new ArrayList<Double>(10));
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
// grades.get().add(); Not sure what to do here?
// If this was an array I would do something like:
// grades[i][j] = 100 -j -i;
}
}
}
Something like this could do?
public void q6()
{
//part a
ArrayList<ArrayList<Double>> grades;
//part b
grades = new ArrayList<ArrayList<Double>>(10);
//second dimension
for(int i = 0; i < 10; i++)
{
List<Double> current = new ArrayList<Double>(10);
grades.add(current);
for(int j = 0; j < 10; j++)
{
current.add(100 - j - i);
}
}
}
Given the code, all you left to do is change it a little to receive 10x10 matrix.
public class Main
{
public static final int ROW_COUNT = 5;
public static final int COL_COUNT = 10;
public static void main(String[] args)
{
ArrayList<ArrayList<Double>> grades = new ArrayList<ArrayList<Double>>();
for (int i = 0; i < ROW_COUNT; i++)
{
ArrayList<Double> row = new ArrayList<Double>();
for (int j = 0; j < COL_COUNT; j++)
{
row.add(100.0 - j - i);
}
grades.add(row);
}
for (int i = 0; i < ROW_COUNT; i++)
{
for (int j = 0; j < COL_COUNT; j++)
{
System.out.print(grades.get(i).get(j));
System.out.print(", ");
}
System.out.println("");
}
}
}

Categories

Resources