0

I'm working on a code to produce a 2D array to hold user inputted values. I made a averaging function in the "print array for loop"but it messes up such as when column input (3 1 2) gives an average of output of (2 6 4). All the values of the array were set to 2 for testing. I can't figure out what is wrong with the loop. I'm new to java so I'm sorry if the answer is something obvious.

The table printed should look like this with row(3) and columns(3 1 2):

A:2.0 2.0 2.0 [3.0]

B:2.0 [2.0]

C:2.0 2.0 [2.0]

where the bracketed term holds the average and the 2.0 are the values held by the column.

The code:

        // creating 2d array

        System.out.print("Please enter number of rows               : ");
        rows = Keyboard.nextInt();
        Keyboard.nextLine();

        while (rows < 0 || rows >= 10) {
            System.out.print("ERROR:Out of range, try again              : ");
            rows = Keyboard.nextInt();
            Keyboard.nextLine();
        }

        double[][] figures = new double[rows][num];

        for(int t = 0; t < rows; t++) { 
            rLetter = (char)((t)+'A');
            System.out.print("Please enter number of positions in row " + rLetter + " : "); 
                columns = Keyboard.nextInt(); 
                Keyboard.nextLine(); 

            while((columns < 0) || (columns >= 8)) { 
                System.out.print("ERROR:Out of range, try again : "); 
                    columns = Keyboard.nextInt(); 
                    Keyboard.nextLine(); 
            } 

            figures[t] = new double[columns]; 
        }

        // filling the array
        for(int row = 0; row < figures.length; ++row) {
            for(int col = 0; col < figures[row].length; ++col) {
                figures[row][col] = 2.0;
            }
        }

        // printing the array
        for(int row=0; row<figures.length; ++row) {
            // printing data row
            group = (char)((row)+(int)'A');
            System.out.print(group+" : ");
            for(int col=0; col<figures[row].length; ++col) {
                sum += figures[row][col];
                average = sum/figures[row].length;

                System.out.print("   "+figures[row][col]);

                System.out.print(" ");
            }

            System.out.printf("%1$5s","["+average+"]");
            System.out.println();
    }

}

PS: its a minor question, I'm using %1$5s to keep the bracket term 5 spaces from the printed columns, but I was wondering if there is a way to keep them all at the same length.

ruhungry
  • 4,116
  • 18
  • 51
  • 93
  • There's far too much code here for me to understand what's going on, but the way you're using `printf` is very odd. Normally, you put all the formatting in the first argument, and the values after that. I would have expected to see `System.out.printf("[%.2f]", average);` or something similar. In other words, put the `[]` in the format string, not the value. – Dawood ibn Kareem Apr 09 '14 at 03:57
  • 1
    I guess this is the **6th** times you posted question with the same code – Baby Apr 09 '14 at 03:57
  • Oh, hang on, I've just noticed that there's a question at the top AND a question at the bottom. Did I deal with the wrong one? It's really best if you stick to one question per question. – Dawood ibn Kareem Apr 09 '14 at 04:00
  • Why do you have the array-printing code twice? – Dawood ibn Kareem Apr 09 '14 at 04:04
  • @DavidWallace he/she does. check all of the OP's question, its all about this code only. -_- – Baby Apr 09 '14 at 04:04

2 Answers2

0

To pad out a string, there's a good answer on here already How can I pad a String in Java?. Look at the 2nd answer.

int minLen = 20;
String s = myformat(value, length);
int diff = minLen - s.length;
System.out.printf("%1$" + diff + "s", s);

Where String s is your formatted string using your above "[" + average + "]" stuff. The idea is that you have a minimum length string to work with so that your positioning is always the same.

Community
  • 1
  • 1
Justin Mitchell
  • 319
  • 1
  • 5
0

You need to add

sum = 0;

after

for(int row=0; row<figures.length; ++row) {

otherwise the totals are wrong when you calculate the average.

Dawood ibn Kareem
  • 68,796
  • 13
  • 85
  • 101