Hye.. I am very new in Java. My question is how to print an array in matrix form. Below is the code for my program:
import java.io.*;
publicclass matrik {
//main() : application entry point
publicstaticvoid main(String[] args) throws IOException {
// create 2D array of dim and initialised
int[][] dim = newint[2][2];
boolean plusminus = false;
boolean multiple = false;
//notes: dim(0,0) = height matrix A dim(0,1) = width matrix A
// dim(1,0) = height matrix B dim(1,1) = width matrix B
//set input stream
BufferedReader stdin =
new BufferedReader(new InputStreamReader(System.in));
//MATRIX A
//ask input(height,width and element(s) for Matrix A
System.out.println("Enter the height of matrix A : ");
dim[0][0] = Integer.parseInt(stdin.readLine());
System.out.println("Enter the width of matrix A : ");
dim[0][1] = Integer.parseInt(stdin.readLine());
//create matrix A || all element(s) is zero
int[][] A = newint[dim[0][0]][dim[0][1]];
//ask for the element(s)
for (int i=0 ; i < A.length ; i++)
for (int j=0 ; j < A[i].length ; j++)
{
System.out.println("Enter element A("+(i+1)+","+(j+1)+") : ");
A[i][j] = Integer.parseInt(stdin.readLine());
}
//MATRIX B
//ask input(height,width and element(s) for Matrix B
System.out.println("Enter the height of matrix B : ");
dim[1][0] = Integer.parseInt(stdin.readLine());
System.out.println("Enter the width of matrix B : ");
dim[1][1] = Integer.parseInt(stdin.readLine()) ;
//create matrix B || all element(s) is zero
int[][] B = newint[dim[1][0]][dim[1][1]];
//ask for the element(s)
for (int i=0 ; i < B.length ; i++)
for (int j=0 ; j < B[i].length ; j++)
{
System.out.println("Enter element B("+(i+1)+","+(j+1)+") : ");
B[i][j] = Integer.parseInt(stdin.readLine());
}
//Check if the matrices can perform addtion and substraction
if ( dim[0][0] == dim[1][0] && dim[0][1] == dim[1][1] )
{ plusminus = true; }
//Check if the matrices can perform multiplication
if ( dim[0][1] == dim[1][0] )
{ multiple = true; }
//Addition
if (plusminus == true)
{
//create matrix Cplus || all element(s) is zero
int[][] Cplus = newint[dim[0][0]][dim[0][1]];
//method to perform addition [(A + B) = Cplus]
for (int i=0 ; i < Cplus.length ; i++)
{ for (int j=0 ; j < Cplus[i].length ; j++)
Cplus[i][j] = A[i][j] - B[i][j]; }
}
//Substraction
if (plusminus == true)
{
//create matrix Cminus || all element(s) is zero
int[][] Cminus = newint[dim[0][0]][dim[0][1]];
//method to perform subtration [(A - B) = Cminus ]
for (int i=0 ; i < Cminus.length ; i++)
{ for (int j=0 ; j < Cminus[i].length ; j++)
Cminus[i][j] = A[i][j] - B[i][j]; }
}
//Mulstiplication
if (multiple == true)
{
//create matrix Cmutiply || all element(s) is zero
int[][] Cmultiply = newint[dim[0][0]][dim[1][1]];
//method to perform multiplication [(A*B) = Cmultilpy]
for (int i=0 ; i < Cmultiply.length ; i++)
for (int j=0 ; j < Cmultiply[i].length ; j++)
for (int k=0 ; k < dim[0][1] ; k++)
{
Cmultiply[i][j] = Cmultiply[i][j] + A[i][k]*B[k][j];
}
}
//Printing the results
//Display Matrix A
//********Here is my problem... ****
//Problem arise if the element is more than one digit
System.out.println("Matrix A");
for (int i=0 ; i < A.length ; i++)
{ System.out.println();
for (int j=0 ; j < A[i].length ; j++)
{System.out.print(A[i][j]+" ");
}
}
}
}