Printing integers with for loops - java

I'm having a strange issue trying to print a 9x9 grid of integers. When I try this code in the main method:
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 0; col++) {
System.out.format( "%d ", entries[row][col] );
}
System.out.print("\n");
}
The output is just a bunch of spaces and newlines, without the actual integers. I've tested 'entries' to make sure it actually contains the correct values (and it does). The weird thing is, when I try the following code also in the main method:
System.out.format("%d ", entries[0][0]);
It works. For some reason the for loop is messing up the output. Any ideas?

You did a mistake in the inner for loop:
for (int col = 0; col < 0; col++)
This wont do any iteration because zero is equals zero.
I think this is what you want:
for (int col = 0; col < 9; col++)

Problem is with the condition in second for loop
for (int col = 0; col < 0; col++) {
It is always false and hence never gets executed.
You should instead use:
for (int col = 0; col < 9; col++) {

This condition is never met:
for (int col = 0; col < 0; col++) {
so you can just simplify it by doing:
for (int row = 0; row < 9; row++) {
System.out.format( "%d ", entries[row][0] );
System.out.print("\n");
}

To print a two-dimensional array , you have to print each element in the array using a loop like the following:
int[][] matrix = new int[9][9];//9*9 grid container
for(int row=0 ; row < matrix.length ;row++){
for(int column= 0 ; column < matrix[row].length;column++){
System.out.print(matrix[row][column] + "");
}
System.out.println();
}

Related

How to print a 2d array horizontally multiple times

I am using java and I would like to print a 2d array horizontally multiple time based on user input. However, my array prints vertically, can anyone help?
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.println();
}
}
For example board array =
xx
xx
I would like
xxxxxx
xxxxxx
If you want to print 2d array horizontally, you have to repeat printing row n times before next row:
int n = 3; // user input
char[][] board = new char[][] { { 'x', 'x', 'x' }, { '0', '0', '0' } }; //example board
for (int row = 0; row < board.length; row++)
{
for (int i = 0; i < n; i++)
{
for (int col = 0; col < board[row].length; col++)
{
System.out.print(board[row][col]);
}
System.out.print("\t"); //arrays separated by tab
}
System.out.println();
}
Output:
xxx xxx xxx
000 000 000
I hope this help.
Your solution works fine except that you made a little mistake in the last lines of your code. I think you want to print a space between every entry by using System.out.println() but println prints a line-break at the end. So your code should look like this:
n=3; //user input
char[][] board = new char[2][3];
char[][] f = new char[board.length][n * board[0].length];
for (int i = 1; i < n + 1; i++) {
int Start = (i * board[0].length) - board[0].length;
int End = i * board[0].length;
for (int row = 0; row < f.length; row++) {
for (int col = nStart; col < nEnd; col++) {
f[row][col] = board[row][col - nStart];
System.out.print(f[row][col]);
}
System.out.print(" "); // Print a space between every single output
}
}
Or if you dont wan't a space at all remove the line completely. Or change the space to a comma, point or whatever you need.
Removing the copied array f.
What you need is exchanging row,col in loop.
n=3; //user input
char[][] board = new char[2][3];
for (int j = 0; j < board[0].length; j++) {
for (int i = 0; i < n ; i++) {
System.out.print(board[i][j]);
System.out.print(" ");
}
System.out.println();
}

Shifting a 2D array to the left loop

I have a 2D array with values in it. Example below:
010101
101010
010101
I want to create a loop that shifts these values to the left like the example below.
101010
010101
101010
So the element that "falls off" goes back in to the end. I'm having a hard time solving this issue in code.
Anyone got any advice?
So far I have made it scroll but I have no clue how to get the elements that fall off to go back in.
This is what I have so far.
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
if (!(row >= array.length) && !(col >= array[row].length - 1)) {
array[row][col] = array[row][col + 1];
}
}
}
Try using the modulus operator:
arrayShifted[row][col] = array[row][(col + 1) % array[row].length];
Remove your condition check as well. Also note, to avoid overwriting values, you'll need to store the results in a new array.
for (int row = 0; row < array.length; row++) {
for (int col = 0; col < array[row].length; col++) {
arrayShifted[row][col] = array[row][(col + 1) % array[row].length]
}
}
Here is a full method that takes in a variable amount of spots to shift each row and properly handles copying the same elements as in the modulus approach.
public void shiftArray(int[][] array, int shift) {
for (int row = 0; row < array.length; row++) {
int rowLength = array[row].length;
// keep shift within bounds of the array
shift = shift % rowLength;
// copy out elements that will "fall off"
int[] tmp = new int[shift];
for (int i = 0; i < shift; i++) {
tmp[i] = array[row][i];
}
// shift like normal
for (int col = 0; col < rowLength - shift; col++) {
array[row][col] = array[row][col + shift];
}
// copy back the "fallen off" elements
for (int i = 0; i < shift; i++) {
array[row][i + (rowLength - shift)] = tmp[i];
}
}
}
Test Run
int[][] array = new int[][] {
{0,1,0,1,0,1},
{1,0,1,0,1,0},
{0,1,0,1,0,1}
};
shiftArray(array, 1);
for (int[] row : array) {
for (int col : row) {
System.out.print(col);
}
System.out.println();
}
// 101010
// 010101
// 101010

Populate a 2D Array with ints

Title is slightly confusing... Sorry.
I have to answer a question, and I started it, but I'm not 100% sure of what is being asked. Perhaps one of you understand it.
Here is the question:
Write the code for populating a 2-D array of ints that models a multiplication table. The array should have 12 rows and 12 columns. Each entry in the ‘table’ should be the product of the row*col -- e.g. The element at arr[2][3] = 6 and the element at arr[0][11] = 0.
Here is what I have so far, but I don't know how to continue:
int arr[][] = new int[12][12];
int mult;
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
}
}
basically a 2d array can store value (for a tutorial click here). What you want to do is save the value in the block it self.
int arr[][] = new int[12][12];
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
arr[row][col]=row*col;
}
}
Then use a double for loop to see the output like:
for(int row = 0; row < arr.length; row++){
for(int col = 0; col < arr[1].length; col++){
System.out.print(row +" * " col + " = "+arr[row][col]+" ");
}
System.out.println();
}

Star staircase confusion

OK I'm embarrassed I have to ask this but I'm stuck so here we go, please nudge me in the right direction.
We need to create this using a nested loop:
*
**
***
****
*****
Here's what I came up with.
int row,col;
for(row = 5; row >= 1; row--)
{
for (col = 0; col < 5 - row; col++)
println("");
for(col = 5; col >= row; col--)
print("*");
}
It ALMOST works but it prints spaces between each row and I cannot for the life of me figure out why.
Why not just one println(""); at the end of the loop instead of looping that statement? You only need the one new line per row of stars.
I think you only want to use one inner loop, to print each row of stars. Then print a newline at the end of each row:
int row, col;
for(row = 1; row <= 5; row++) {
for(col = 0; col < row; col++) {
print("*");
}
println("");
}
Why don't you do this:
for (int row = 0; row < 5; row++) {
for (int col = 0; col <= row; col++) {
System.out.print("*");
}
System.out.print("\n");
}
Why don't you simplify it?
int row, col;
for(row=0; row<5; row++) {
for(col=0;col<=row;col++) {
System.out.print("*");
}
System.out.println();
}
Print one row at a time, and then print the line-end
For the sake of only one I/O operation, a function may be a suitable approach:
public String starStarcase(int rows) {
int row, col;
String str="";
for(row = 0; row < rows; row++) {
for(col = 0; col <= row; col++) {
str += "*";
}
str += "\n";
}
return str;
}
To print the result:
System.out.println(starsStaircase(5));
You can try this, it should work:
for(int j = 0; j < row; j++){
for(int i = 1; i <= row; i++){
System.out.print(i < row-j ? " " : "#");
}
System.out.println("");
}

Display matrix data

I want to display matrix values just like a matrix
just like this
0 0 1 0 0
0 1 1 1 1
0 0 0 0 0
When I make this code, it appears vertically
for (int i = 0; i < mat.length; i++)
for (int j = 0; j < mat[i].length; j++)
System.out.println(mat[i][j]);
System.out.println prints a line and a break line. You would want to use System.out.print that only prints the data.
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
System.out.print(mat[i][j]);
}
System.out.println();
}
But now the problem is that all the data in the same row is printed with no space. You can use System.out.print(mat[i][j] + " "); but that's a little clumsy. The best option would be to use System.printf to allow formatting of the text that will be printed on the console:
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[i].length; j++) {
//assuming mat[i][j] content is int...
System.out.printf("%3d", mat[i][j]);
}
System.out.println();
}
More info about formatting with System.out.printf: Format String syntax
Try this: Use print in inner loop which will print one row in a single line. Use println in outer loop which will separate each row.
for (int i = 0; i < mat.length; i++){
for (int j = 0; j < mat[i].length; j++)
System.out.print(mat[i][j]);
System.out.println();
}

Categories