Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi! This is my code: public class TestCodes { public static void main (String []

ID: 3649096 • Letter: H

Question

Hi! This is my code:

public class TestCodes
{
public static void main (String [] args)
{


//Array
int [][] a = {{10,100,101,-1000},
{1000,10,55,4},
{1,-1,4,0}};

//Num of rows and columns
int N = a.length;
int M = a[0].length;

//Initialize variables
int max = a[0][0];
int min = a[0][0];

int cellSpace = 0; //Spacing based on longest element in this Matrix

//Find the max and min values in the array
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
{
if(a[i][j] > max)
max = a[i][j];

if(a[i][j] < min)
min = a[i][j];
}

//Get the lengths of strings of the max and min values
int lengthOfMax = ("" + max).length();
int lengthOfMin = ("" + min).length();

//compare lengths to get the cell space of the matrix output
if(lengthOfMax > lengthOfMin)
cellSpace = lengthOfMax + 1;
else
cellSpace = lengthOfMin + 1;

String longStr = "";


//Print formatted matrix based on the largest string length
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
String form = "%1$" + cellSpace + "d"; //format of the string based on cellSpace,
String output = String.format( form, a[i][j]); //formatted int value converted to string
longStr += output; //makes a long string of all the values in the array
}
}

System.out.print(longStr); //<--HOW CAN I GET THIS TO PRINT CORRECTLY!

}
}

The output of the above is:

10 100 101 -1000 1000 10 55 4 1 -1 4 0

The cellSpace is perfect, except I need it to look like this:

| _ _ _ _ 1 0 _ _ _ 1 0 0 _ _ _ 1 0 1 _ - 1 0 0 0 |
| _ _ 1 0 0 0 _ _ _ _ 1 0 _ _ _ _ 5 5 _ _ _ _ _ 4 |
| _ _ _ _ _ 1 _ _ _ _ -1 _ _ _ _ _ 5 _ _ _ _ _ 0 |

//the "_" represents 1 space. So since -1000 has the longest string length of 5 ,we add 1, and the cellSpace of all must be 6.

So basically:
I need to insert "|" at the beginning and end of each row
and I need to break the longStr using " " somehow so when it prints it looks like the matrix.


10 100 101 -1000 1000 10 55 4 1 -1 4 0

So break it up ^ ^ ^

Is there a String method in the String class that I can use to insert "|" at the beginning and end of each row and break up the long str according to the number or rows and columns?
Can somebody explain a step by step guide on how to accomplish this result by adding to my code or a ?

Explanation / Answer

please rate - thanks

public class TestCodes
{
public static void main (String [] args)
{


//Array
int [][] a = {{10,100,101,-1000},
{1000,10,55,4},
{1,-1,4,0}};

//Num of rows and columns
int N = a.length;
int M = a[0].length;

//Initialize variables
int max = a[0][0];
int min = a[0][0];

int cellSpace = 0; //Spacing based on longest element in this Matrix

//Find the max and min values in the array
for(int i = 0; i < N; i++)
for(int j = 0; j < M; j++)
{
if(a[i][j] > max)
max = a[i][j];

if(a[i][j] < min)
min = a[i][j];
}

//Get the lengths of strings of the max and min values
int lengthOfMax = ("" + max).length();
int lengthOfMin = ("" + min).length();

//compare lengths to get the cell space of the matrix output
if(lengthOfMax > lengthOfMin)
cellSpace = lengthOfMax + 1;
else
cellSpace = lengthOfMin + 1;

String longStr = "";

/*
//Print formatted matrix based on the largest string length
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
String form = "%1$" + cellSpace + "d"; //format of the string based on cellSpace,
String output = String.format( form, a[i][j]); //formatted int value converted to string
longStr += output; //makes a long string of all the values in the array
}
}
*/
longStr=formatMatrix(a);
System.out.print(longStr); //<--HOW CAN I GET THIS TO PRINT CORRECTLY!

}
public static String formatMatrix(int mat[][])
{String out="";
int i,j, max=0,chars=0;
for(i=0;i<mat.length;i++)
    for(j=0;j<mat[0].length;j++)
         if(Math.abs(mat[i][j])>max)
              max=Math.abs(mat[i][j]);
chars=countDigits(max)+1;
for(i=0;i<mat.length;i++)
     {out=out+"|";
      for(j=0;j<mat[0].length;j++)
           out=out+formatIt(mat[i][j],chars);
      out=out+"| ";
     }
return out;
}
public static String formatIt(int n,int c)
{String o="";
int i;
int m=countDigits(Math.abs(n));
int spaces=c-m+1;
if(n<0)
    spaces--;
for(i=0;i<spaces;i++)
    o=o+" ";
o=o+n;

return o;
}
public static int countDigits(int n)
{int count=0;
if(n<0)
     count=1;
while(n>0)
   {count++;
    n/=10;
    }
if(count==0)
    count=1;
return count;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote