2

I need to print a two dimensional array with string values like a table.

So far my code is:

import java.util.Arrays;

public class Employee {
    public static void main(String[] args) {
        String wages[][] =
            {
                {"Emp", "Hours", "Wages"},
                {"Bobby", "45", "35"},
                {"Rick", "15",  "33"},
                {"Mike", "66", "50"},
                {"Jayme", "15", "45"}
         };         

       System.out.print(Arrays.deepToString(wages));
       System.out.println();
    }
}

The output is:

[[Emp, Hours, Wages], [Bobby, 45, 35], [Rick, 15, 33], [Mike, 66, 50], [Jayme, 15, 45]]

I need it to look more like a table with separate rows (5 rows 3 columns)

Barney
  • 1,851
  • 2
  • 19
  • 36
  • 1
    Then you need to iterate the array manually and print just what you need when you need it. – takendarkk Feb 01 '18 at 16:11
  • You can warp those in an object You can implement the method toString to format your output – JPRLCol Feb 01 '18 at 16:12
  • 1
    There appear to be [some good answers to a similar question](https://stackoverflow.com/questions/7782080/java-printing-two-dimensional-array) available. – Van Boughner Feb 01 '18 at 16:13

2 Answers2

4

You have to iterate in your array and print all entries like this.

String wages[][] = {
    {"Emp", "Hours", "Wages"},
    {"Bobby", "45", "35"},
    {"Rick", "15",  "33"},
    {"Mike", "66", "50"},
    {"Jayme", "15", "45"}
};

Arrays.stream(wages).map(Arrays::toString).forEach(System.out::println);

The output

[Emp, Hours, Wages]
[Bobby, 45, 35]
[Rick, 15, 33]
[Mike, 66, 50]
[Jayme, 15, 45]

Try it online !

Valentin Michalak
  • 2,089
  • 1
  • 14
  • 27
1

A very simple solution is like this:

for (int i = 0; i < wages.length; i++) {
    for (int j = 0; j < wages[i].length; j++) {
        System.out.print(wages[i][j]);
    }
    System.out.println();
}

If the length of the strings cause a problem in displaying, then you could do something like this:

import java.util.Arrays;

public class Employee {

    public int[] getMaxLength(String wages[][]) {
        int lengths[] = new int[wages[0].length];
        for (int i = 0; i < lengths.length; i++) lengths[i] = 0;
        for (int i = 0; i < wages.length; i++) {
            for (int j = 0; j < wages[i].length; j++) {
                if (wages[i][j].length() > lengths[j]) lengths[j] = wages[i][j].length();
            }
        }
        return lengths;
    }

    public static String friendlyString(String input, int size) {
        while (input.length() < size) input += " ";
        return input;
    }

    public static void main(String[] args) {
        String wages[][] =
            {
                {"Emp", "Hours", "Wages"},
                {"Bobby", "45", "35"},
                {"Rick", "15",  "33"},
                {"Mike", "66", "50"},
                {"Jayme", "15", "45"}
         };         

        int sizes[] = getMaxLength(wages);
        for (int i = 0; i < wages.length; i++) {
            for (int j = 0; j < wages[i].length; j++) {
                System.out.print(friendlyString(wages[i][j], sizes[j]));
            }
            System.out.println();
        }
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • The first solution worked perfectly for me, thank you. – Ryan Asciutto Feb 01 '18 at 19:35
  • @RyanAsciutto you are welcome. If the solution solved your problem, you might consider accepting this answer as the correct one by clicking on the check mark to the left of it, so future users might be helped by it as well. – Lajos Arpad Feb 01 '18 at 19:36