Thread: Pproblem aligning arrays

1. Member Join Date
Nov 2013
Posts
2
Rep Power
0 Pproblem aligning arrays

Hi everyone. I have been literally pulling my hair of frustration for not being able to properly align using the systemformat for Java . My instructor wants the arrays to be aligned (right justified)especially if the row and column are greater than 10 along with the = signs and the answer. Because since my program runs on scanner, he will be able to put a big row and column like 15 x 15 and the arrays along with the answer should align and be right justified. I will appreciate any help // how my code looks like as you can see it's not aligned

//source. The problem lies in my DisplayArray method
Java Code:

public class JDouble
{
public JDouble(){}

public JDouble(int A, int B)
{
Row = A;
Col = B;
Number = new int [A][B];
}

void ZeroThem() //intitializing to 0
{
int k,m;
for ( k = 0; k < Row; k++)
{
for ( m = 0; m < Col; m++)
{
Number[k][m] = 0;
}
}
}
void PopulateEvrtngElseSeven(int A) // Populating the rest of rows with seven
{
int k,m,Holder;

k = 0;
while ( k < Row)
{
m = 0;
while ( m < Col )
{
Holder = A;
if ( Holder == A )
{
Number[k][m] = Holder; //will populate everything else with seven
m++;
}
}
k++;
}
}

void PopulateRowSevenOdd() // I want to populate the seventh row with 3 digit odd numbers
{
int k,Holder;

k = 0;

while ( k < Col  )
{
Holder = (int) (Math.random() * 1000 ) ;

if ( Holder > 99 && Holder % 2 != 0)
{
Number[k] = Holder; // will populate onlyl in 7th row
k++;
}
}
}

void PopulateRowTwoEven() // I want to populate the second row with 2 digit even numbers
{

int k, Holder;

k = 0;

while ( k < Col  )
{
Holder = (int) (Math.random() * 1000 ) ;

if ( Holder < 100 && Holder % 2 == 0 && Holder > 9)
{
Number[k] = Holder; // will populate only in the 2nd row
k++;
}
}
}

void PopulateRowFourDivisible() // I want to populate the fourth row with 4 digits divisible by 1,2,3,6,12
{
int k,Holder;

k = 0;

while ( k < Col )
{
Holder = (int) (Math.random() * 9000 ) ;

if ( Holder > 999 && Holder % 12 == 0)
{
Number[k] = Holder; // will populate only in 3rd row
k++;
}
}
}

void DisplayArray()//Displaying the arrays. Here is where the aligning happens which i can't figure out
{
int k,m;
for ( k = 0; k < Row; k++)
{
for ( m = 0; m < Col; m++)
{

System.out.format("The Arrays values are Number["+k +"]  ["+m+"] = "+ " %11d%n ", Number[k] [m ] );

}
}

}

void DiplayMaxMin() // We are going to find the biggest,smallest,third biggest,and fourth smallest array here
{
int k;
int m;

int max;
int SecondMax;
int ThirdMax;

int min;
int SecondMin;
int ThirdMin;
int FourthMin;

min = Number[ 0 ]; // This sets the first element of the array as the minimum.
max = min;          // Setting the first element also to be the max. Along with the rest.
SecondMax = min;
ThirdMax  = min;

SecondMin = min;
ThirdMin = min;
FourthMin = min;
// Even though we will be checking these elements again in the loop, since you are using a 2 dimensional array it is unavoidable.

int Holder;

for (k = 0; k < Row; k++)
{
for (m = 0; m < Col; m++)
{
Holder = Number[k] [m];
if (Holder < min) // this will find the lowest number
{
FourthMin = ThirdMin;
ThirdMin = SecondMin;
SecondMin = min;
min = Holder;

}
else if (Holder < SecondMin)//second lowest number
{
FourthMin = ThirdMin;
ThirdMin = SecondMin;
SecondMin = Holder;

}
else if (Holder < ThirdMin)//third lowest number
{
FourthMin = ThirdMin;
ThirdMin = Holder;
}
else if (Holder < FourthMin)//fourth lowest number
{
FourthMin = Holder;
}

if (Holder > max)// this will find the max number
{
ThirdMax = SecondMax;
SecondMax = max;
max = Holder;
}
else if (Holder > SecondMax)//second max number
{
ThirdMax = SecondMax;
SecondMax = Holder;
}

else if(Holder > ThirdMax)//third max number
{
ThirdMax = Holder;

}

}
}
System.out.println("\n\n");
System.out.format("\t\tThe biggest Array in Number        = " + "%7d%n", max  );
System.out.format("\t\tThe third biggest Array in Number  = " + "%7d%n", ThirdMax);

System.out.format("\t\tThe smallest Array in Number       = " + "%7d%n", min);
System.out.format("\t\tThe fourth smallest Array in Number= " + "%7d%n", FourthMin);

}

private int Number[][];
private int Row;
private int Col;
}
//calling program
Java Code:
import java.util.*;

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

Scanner READ = new Scanner(System.in);
int A,B,C;

// A = Row, B = Col, C = how many digits

System.out.print("\n\t\tGive me number of Rows : ");

System.out.print("\n\t\tGive me number of Cols : ");

System.out.print("\n\t\tGive me number of Digs : ");

JDouble Test = new JDouble(A,B);

Test.PopulateEvrtngElseSeven(C);
Test.PopulateRowSevenOdd();
Test.PopulateRowTwoEven();
Test.PopulateRowFourDivisible();
Test.DisplayArray();
Test.DiplayMaxMin();

System.out.println("\n\n\n");
}
}
Last edited by hungry_mouth; 11-18-2013 at 02:23 AM.  Reply With Quote

2. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13 Re: Pproblem aligning arrays

Your alignment will always be thrown off because it is preceded by strings that are printed without any format and are not consistent in length. Namely, the values you print in brackets. So you need print those as strings using a fixed width string format similar to your fixed width number format.

Regards,
Jim  Reply With Quote

3. Member Join Date
Nov 2013
Posts
2
Rep Power
0 Re: Pproblem aligning arrays

So i would i need to use the string format to align the brakets and then use the system format to print my values then? I will do that and see if that works. Thank you.  Reply With Quote Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•