Formatting array output into 9x9 box - java

I had to write a program for my class that creates a 9x9 multiplication table. I've written the program to where the numbers are correct but I don't know how to format it to where the output is in a 9x9 box. Thanks for the help in advance.
import java.io.*;
import java.util.Scanner;
public class MultTable
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
int multTableSize = 9;
int[][] multTable = new int[multTableSize][multTableSize];
for (int row = 0; row < multTable.length; row++)
{
for (int column = 0; column < multTable[row].length; column++)
{
multTable[row][column] = (row + 1) * (column + 1);
System.out.print(multTable[row][column] + " ");
}
}
System.out.println();
}
}

First, you are adding the line break outside both of your loops, rather than at the end of the first loop, and second, for text formatting, you are better off using printf() than println().
Try something like this:
for (int row = 0; row < multTable.length; row++) {
for (int column = 0; column < multTable[row].length; column++) {
multTable[row][column] = (row + 1) * (column + 1);
System.out.printf("%3d", multTable[row][column]);
}
System.out.println();
}
%3d is a format string which will make your numbers appear in 3-character wide columns.
See the Formatter class docs to learn more about how to format output in Java.

Perhaps use a System.out.println(); after your inner loop?

for (int row = 0; row < multTable.length; row++)
{
for (int column = 0; column < multTable[row].length; column++)
{
multTable[row][column] = (row + 1) * (column + 1);
System.out.print(multTable[row][column] + " ");
}
System.out.println();
}
after the end of the second loop --> you need to skip one line..

Related

Patterns using for loops

I am supposed to create this pattern based on the number a user enters. The number the user enters corresponds to the number of rows and stars in each row.
*
**
***
****
*****
I was told to only use nested for loops and cannot use printf(). This is only part of the code that I am working on.
for (int row = 1; row <= size; row++) {
for (int column = 1; column <row; column++) {
System.out.print(" ");
}
for (int column = 1; column <= row; column++) {
System.out.print("*");
}
System.out.println();
}
I cannot seem to make my output as shown above. Instead I get this:
*
**
***
****
*****
Could someone give me a hint as to what I am supposed to do? I have spent 2 hours but still can't figure it out.
For each row, you should output maximum size characters; so if size = 5, on third row, if output three stars, then you need size-row spaces => 5 - 3 = 2.
In code:
for (int row = 1; row <= size; row++) {
for (int column = 1; column <= size-row; column++) {
System.out.print(" ");
}
for (int column = 1; column <= row; column++) {
System.out.print("*");
}
System.out.println();
}
Sample: http://ideone.com/hC5HDQ
You want to start with more spaces, then remove them until there is not more left. But while removing spaces, you also want to add and additional " * ". So for every space removed you will add an " * "
Try this
int i=5;
do{
int j=5;
while(j>i){
System.out.print("*");
j--;
}
System.out.println();
i--;
}while(i>0);

how to print the row numbers in a 2d array game board in java

I am working on a java Othello game and am using a 2D array with padding to build the board. I have the board printing just fine, the columns are labeled "a -h" but i need the rows to be numberd "1-8" and cannot figure out how to do this. my code is as follows:
void printBoard() {
String results = "";
OthelloOut.printComment(" a b c d e f g h");
int row = board.board.length;
int col = board.board[0].length;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
}
the othelloOut class sort of extends the System.out print statements
public class OthelloOut {
static public void printMove(PieceColor color, Move amove){
System.out.printf("%s %s\n", color, amove);
}//printMove
static public void printComment(String str){
System.out.printf("C %s\n", str);
}//printComment
static public void printReady(PieceColor color){
System.out.printf("R %s\n", color);
}//printReady
}//OthelloOut
any help will be much appreciated. If this needs to be clarified more just let me know! thanks.
UPDATE: Numbers print but i prints 0 - 9 and i want it to skip the digits 0 and 9 to where its blank in the positions of those two numbers. Any suggestions? Thanks for the help guys!
Your best bet is doing it here:
for (int i = 0; i < row; i++) {
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}
Remember that you're not using println, you're using print. You want all other text to be printed onto the same line as i.
And while I'm here..
I would be using a StringBuilder, instead of concatenating a String.
for (int i = 0; i < row; i++) {
StringBuilder results = new StringBuilder();
OthelloOut.printComment(i); // Obviously not exactly like this.
for (int j = 0; j < col; j++) {
results.append(pieces[board.board[i][j] + 2]);
}
OthelloOut.printComment(results.toString());
}
You can add the row number at each row iteration like this:
for (int i = 0; i < row; i++) {
results += i + 1; // add the row number
for (int j = 0; j < col; j++) {
results += " " + pieces[board.board[i][j] + 2];
}
OthelloOut.printComment(results);
results = "";
}

Java printing unicode characters in multidimensional arrays in columns + rows?

I have a small project to complete on my course but I'm a little stuck on solving this, basically I need to print the unicode characters in a multidimensional array to a table, 12 rows and 5 columns. So far I have this:
public class MultiArrTest {
public static void main(String[] args0) {
char[][] uc = new char[12][5];
int x = 64;
for (int i = 0; i < uc.length; i++) {
for (int j = 0; j < uc[i].length; j++) {
uc[i][j] = (char) x++;
System.out.print(uc[i][j] + " ");
System.out.println();
}
}
}
}
This prints the unicode but only in one column, I feel a bit silly here but could anyone give me a suggestion?
Thanks a lot.
Move the System.out.println(); outside the second for-loop and inside the first one, just after the for-loop:
for (int i = 0; i < uc.length; i++) {
for (int j = 0; j < uc[i].length; j++) {
uc[i][j] = (char) x++;
System.out.print(uc[i][j] + " ");
}
System.out.println();
}

Printing a centered pyramid in Java with ascending and descending numbers

I'm trying to print a centered pyramid of 2^n, where 2^row# is the centered number of each row, the numbers to the left are ascending to 2^row# and the numbers to the right are descending. I'm pretty new to Java and it took me a really long time to get this much. But now I'm stuck. The last row is the only row that is correct. I don't know how to make it so 64 is not printed on every line. Can anyone please give me a hint?
I've tried messing with every single parameter - starting the last loop with the first row, the last row, changing the starting power, etc. and I just can't figure it out.
Thank you for any hints!
public static void main (String [] args){
int row;
for (row = 0; row <= 8; row++){ // Prints each row
for (int spaces = 8; spaces >= row; spaces --){ // Prints out spaces to left
System.out.print(" ");
}
int power1 = 0; // Power that 2 is being raised to
for (int i = 0; i < row; i++) { // Prints left side of the pyramid
System.out.print(" " + (int)Math.pow(2, power1));
power1++;
}
int power2 = 7;
for (int i = 1; i < row; i++) { // Prints right side of the pyramid
power2--;
System.out.print(" " + (int)Math.pow(2, power2));
}
System.out.println();
}
}
}
Your problem lies in the fact you always start the right side of the pyramid at 2^7, since you hard code the power2 = 7 decleration and assignment. If you start this value instead at the current row - 1, you get the behavior you're looking for. Code:
public static void main (String [] args){
int row;
for (row = 0; row <= 8; row++){ // Prints each row
for (int spaces = 8; spaces >= row; spaces --){ // Prints out spaces to left
System.out.print(" ");
}
int power1 = 0; // Power that 2 is being raised to
for (int i = 0; i < row; i++) { // Prints left side of the pyramid
System.out.print(" " + (int)Math.pow(2, power1));
power1++;
}
int power2 = row - 1;
for (int i = 1; i < row; i++) { // Prints right side of the pyramid
power2--;
System.out.print(" " + (int)Math.pow(2, power2));
}
System.out.println();
}
This part is not right.
int power2 = 7;
for (int i = 1; i < row; i++) { // Prints right side of the pyramid
power2--;
System.out.print(" " + (int)Math.pow(2, power2));
}
On row 2 you get power2=6 so you display 2^6=64.
You should instead be doing something like
int power2 = power1;
for (int i = 1; i < row; i++) { // Prints right side of the pyramid
power2--;
System.out.print(" " + (int)Math.pow(2, power2));
}
You are assigning constant to power2 instead of depending value on row. Can you try this please.
int power2 = row-1;

little java help with loops

Hi I am doing some practice problems and trying to print a diagonal line like the example below. I have writen the program you see below and I honestly dont understand what I am doing wrong. I m a java beginner and I cant see how to find the error.
Example:
*
*
*
*
*
code:
class Diagonal{
public static void main(String args[]) {
int row, col;
for(row = 1; row < 6; row++) {
for(col = 1; col <= row; col++) {
if(col==row){
System.out.print("*");
} else{
System.out.print("");
}
System.out.println();
}
}
}
}
I am trying to learn for loops because they really confuse me. Another practice is to print a similar diagonal line but this time from right to left. I cant do that without getting this right however :( I believe they will be pretty similar?
Above my reasining is this: As long as the column # is the same as the row number the print the line or otherwise leave a blank....what's wrong with how i did it?
THANK YOU!
You never print any space character. You print an empty String. Replace
System.out.print("");
with
System.out.print(" ");
Also, you write a newline after each column rather than writing one after each row.
String spaces = "";
for(int row = 1; row < 6; row++) {
System.out.println(spaces+"*");
spaces += " ";
}
Print new lines when entering a star: System.out.println("*");
Add spaces: System.out.println(" ");
remove the line where you print new lines between columns.
as said before replace black with space and move the end line to the end of the FIRST for() like this:
class Diagonal{
public static void main(String args[]) {
int row, col;
for(row = 1; row < 6; row++) {
for(col = 1; col <= row; col++) {
if(col==row){
System.out.print("*");
} else{
System.out.print(" ");
}
}
System.out.println();
}
}
}

Categories

Resources