Question:
Java help, with arrays?
=)
2014-04-06 13:49:51 UTC
Does anybody know how to print row and column numbers? I'm trying to make a board, kind of like this:

1 2 3 4 5 6 7 8 9 10
1 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
3 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
4 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
5 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
6 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
7 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
8 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
9 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
10 ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

Also, does anybody mind if I can email questions about a project that I have to do? I'm a beginner in Java, so this is really hard for me.
Three answers:
husoski
2014-04-06 15:00:05 UTC
The natural structure in Java to implement a square board is a (square) two dimensional array:



char board[][] = new char[10][10];



I'm using char here, but you can use int or String or anything other type you like for the array element.



The usual interpretation is that board[i] represents row i (or i+1 in your case since you start numbering at 1, but array index values always start at 0 for the first element) and that board[i][j] then contains the value for row i, column j.



The i and j index names are math style, which I use a lot. For a board, though, I usually use row and col instead. Feel free to spell out "column" if it gets you a better grade.



You can loop for row=1 to 10 and adjust for 0-based indexing when accessing board[][], or you can use 0-based indexing for row and adjust when printing out values. I like the second approach.



I also like using the lengths stored in the board array to control loops, so I don't have to mess with changing a bunch of 10s to 8 or 12 if the board size changes. For that:

board.length = number of rows in the board

board[row].length = number of columns in row (row+1)



Yes, each row can be a different length. They won't be different if you use new char[10][10], but there are other ways to allocate and initialize an array.



Processing the whole board is nearly always done with nested loops, like this loop to initialize the board:



for (int row=0; row
.... for (int col=0; col<=board[row].length; ++col) {

.... .... board[row][col] = '~';

.... }

}



That loads the board with tildes. The .... tokens are for visual indentation. Pretend they're spaces.



The outer loop repeats once for each row. The inner loop repeats once for each column for a given row. I used the



The printout uses the same kind of loop, but with a few extra wrinkles for formatting. First, though, you need a heading that will line up with the board output in the loops. You could write out a single string, but I like to use the size of the board to drive this loop. What I will do is allow 2 positions for each row number so that spacing doesn't get messed up with 10+ rows. Ditto for columns, of course.



System.out.print(" "); // 4 space in quotes!

for (int col=0; col
.... System.out.printf(" %2d", col+1);

}

System.out.println();



That prints out the header line, with printf instead of print used to format column numbers with a space in front and 2 positions for the column number. That looks much line what will be done on each row, but the leading 4 spaces will be a row number, a space, and a "|" or other separator:



for (int row=0; row
.... System.out.printf("%2d |", row+1); // print row number 1-10

.... for (int col=0; col
.... .... System.out.printf(" %2s", board[row][col]);

.... }

.... System.out.println(); // end the output line after whole row is printed

}



That's just entered on-the-run, so expect a typo or two. You'll probably have something else for your array type and slightly different formatting, but this should get you to SOME kind of output that you can adjust to taste.
Jim
2014-04-06 21:14:52 UTC
it's not going to be quite as simple as either of those. 2nd answer is not going to work.

• there needs to be 2 spaces at the beginning of the first line otherwise your

• you need to zero-pad or space-pad your columns so they are even. so you are going to need a padding function that converts an integer to a string and then pads it, and another padding function that takes a string and space-pads it (or center-justifies it or left-justifies it, your choice). the number 10 takes up 2 character columns while the number 0-9 take up only 1 that's why the space padding. otherwise it just looks strange.



take some thought on how you want to approach the problem, then solve it. I notice you have spaces between the numbers and also between the tildes (~). there are methods for converting to and from Integer to String like toString(n)

you will also need String.repeat() for doing the padding, where the number of characters is the pad length - length of toString(n)

http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringUtils.html#repeat%28java.lang.String,%20int%29



http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html?is-external=true
2014-04-06 20:59:20 UTC
You can email me if you want and I'll help.



To print a row of numbers you would do:



for (int i=1;i<=10;i++) { System.out.print(i); }



(think that should work)

for the vertical ones do the same thing but with System.out.println instead of System.out.print



Anyway ya i'd be happy to help you out with java problems if you wanted to shoot me an email : )


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...