0

Hi I am trying to print a two dimensional array that is center aligned but the numbers point to the memory cell if im correct. How would I go about getting them to print the actual numbers, Ive tried creating a display method and that didnt work. Here is my code so far. I am also going to be finding the min, max and avg after I figure this out.

import java.util.Scanner;

public class Print2DArray {

public static void main(String[] args) {

    Scanner print2d = new Scanner(System.in);
    System.out.println("Please enter the number of rows: ");
    int rows = print2d.nextInt();
    System.out.println("Please enter the number of columns: ");
    int columns = print2d.nextInt();

    int array[][] = new int[rows][columns];

    System.out.println("\nVictor  - 002017044\n");

    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            int value = (int) (Math.random() * 10000);
            value = (int) (Math.round((value * 100)) / 100.0);
            array[x][y] = value;
        }
    }

    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            printArray(array);
        }
        System.out.println();
    }


    int max = 0;
    int avg = 0;
    int min = 0;

    System.out.println("\nMaximum: " + max + "\nAverage: " + avg
            + "\nMinimum: " + min);
}

private static void printArray(int [][] array){
    int width = 6;
    int leftSP = (width - array.length)/2;
    int rightSP = width - array.length - leftSP;
    for (int i = 0; i < leftSP; i++) {
        System.out.print(" ");
    }
        System.out.print(array);
    for (int i =0; i < rightSP; i++) {
        System.out.print(" ");
    }
}
}
th3r1singking
  • 119
  • 1
  • 3
  • 13
  • What does `center aligned` mean? You are creating a *rectangle* of numbers. Post the expected result using a small array. – PM 77-1 Jul 04 '14 at 01:46
  • I mean that there would even number of spaces between all of the number in columns and rows like this http://tinypic.com/r/2a7bp1k/8 – th3r1singking Jul 04 '14 at 01:50
  • OK. For this you just need to use [**formatting**](http://docs.oracle.com/javase/tutorial/java/data/numberformat.html). – PM 77-1 Jul 04 '14 at 01:52
  • What is center aligned? Also if you just want to print the array why did u use the separate printArray function? instead of the function call u could just print the values right ? i.e Replacing printArray(array); with System.out.print(array[x][y]); – Crusaderpyro Jul 04 '14 at 01:54

2 Answers2

2

I'm not quite sure what you mean by center aligned in this context. Centering something requires you to know how much space you are allowed to print to and then evenly distributing what you are displaying to both sides of the width/2. For example, by default, in cmd.exe you are limited to 80 characters across, but this can be changed. However, I think the core of this answer is here: Can I find the console width with Java?

Basically, you can't center it. The best you can hope for is to left align it (or attempt to center it based on some arbitrary pre-determined width).

Based on what you wrote though, and what I see in printArray, your other issue is that you don't know how to print out a value at an index of an array. Before I address that, I must address something you wrote

but the numbers point to the memory cell if im correct

This is actually incorrect. This is the default functionality of the toString method, per the java.lang.Object#toString method:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString%28%29

Your print method should probably look like:

private static void printArray(int [][] array){
    if(array == null || array.length < 1 || array[0].length < 1)
        throw new IllegalArgumentException("array must be non-null, and must have a size of at least \"new int[1][1]\"");
    for (int i = 0; i < array.length; i++) {
        for(int j = 0; j < array[0].length; j++)
            System.out.print("[" + array[i][j] + "]");
        System.out.println();
    }
}

EDIT: I saw a comment you made in which you specify what you mean by center aligned. Basically you will want to record the maximum length of any int you are placing into the array, like the following:

//global max value
public static int maxLength = 0;
...
    //inside of Print2DArray.main(String [] args)
    for (int x = 0; x < array.length; x++) {
        for (int y = 0; y < array[x].length; y++) {
            value = (int) (Math.round((value * 100)) / 100.0);
            int numberOfDigits = String.valueOf(value).length();
            if(numberOfDigits > Print2DArray.maxLength)
                Print2DArray.maxLength = numberOfDigits;
            array[x][y] = value;
        }
    }

Then you will want to adjust your printArray function's print from

System.out.print("[" + array[i][j] + "]");

to

System.out.printf("[%" + Print2DArray.maxLength + "d]", array[i][j]);
Community
  • 1
  • 1
searchengine27
  • 1,449
  • 11
  • 26
0

first fix bug

for (int y = 0; y < array[x].length; y++) {
    printArray(array[x][y]);//chage printArray(array) to printArray(array[x][y])
}

second modify printlnArray

private static void printArray(int v){
     System.out.format("%-8s",String.valueOf(v));
}

center-align

private static int[] widthes = {9,99,999,9999,99999};
private static int numberWidth(int v) {
    for (int i = 0; i < widthes.length; i++) {
      if(widthes[i] > v) return (i+1);
  }
    return -1;
}
private static void printArray(int v){
    int width = 8;
    int numberWidth = numberWidth(v);
    int left = (width-numberWidth)/2;
    int right = width - numberWidth - left;
    for (int i = 0; i < left; i++) {
        System.out.print(" ");
  }
    System.out.print(v);
    for (int i = 0; i < right; i++) {
        System.out.print(" ");
    }
}

more reference How to align String on console output

Community
  • 1
  • 1
Nile
  • 386
  • 5
  • 15
  • this would work but this only aligns it right or left, im trying to center the numbers. – th3r1singking Jul 04 '14 at 02:39
  • there is no exactly center-align. for example. show 123 in 8 width cell. padding 2 space left,and 3 right; – Nile Jul 04 '14 at 03:05
  • if u use groovy. Groovy add a center method to String http://groovy.codehaus.org/groovy-jdk/java/lang/String.html#center%28java.lang.Number,%20java.lang.String%29 – Nile Jul 04 '14 at 03:10
  • I figured out my problem @Sam. All I had to do was convert the int to a string then modify the printfield method to take in a string and an int(the width). It was a pretty simple fix now that I think about it. – th3r1singking Jul 04 '14 at 13:29