Results 1 to 3 of 3
Thread: Pproblem aligning arrays
 11182013, 02:19 AM #1Member
 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[6][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[1][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[3][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][ 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; }
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 : "); A = READ.nextInt(); System.out.print("\n\t\tGive me number of Cols : "); B = READ.nextInt(); System.out.print("\n\t\tGive me number of Digs : "); `C = READ.nextInt(); 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; 11182013 at 02:23 AM.
 11182013, 02:27 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 2,935
 Rep Power
 4
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 11182013, 02:37 AM #3Member
 Join Date
 Nov 2013
 Posts
 2
 Rep Power
 0
Similar Threads

Need help for aligning JButton(to fit more text in less space)
By BenMoon in forum AWT / SwingReplies: 1Last Post: 06252013, 06:35 PM 
Problem with aligning components
By kedecr in forum New To JavaReplies: 2Last Post: 03092011, 01:23 PM 
aligning numbers,..
By rexson98 in forum New To JavaReplies: 6Last Post: 10092008, 01:59 PM 
Having Trouble Aligning JLabels
By Mark_Petrov in forum AWT / SwingReplies: 0Last Post: 01202008, 05:22 PM 
Aligning Labels
By Java Tip in forum Java TipReplies: 0Last Post: 01022008, 06:26 PM
Bookmarks