Hi i gotta display 2 arrays on screen but they don't look good
i was wondering how can i make it look like this:

Speed     20 88  75  80  68  77  81
Focus     30 90  77  83  71  84  85
Jetta     40 94  80  85  76  91  90
Contour   50 102 86  94  85  98  96
Tsuru     60 111 94  100 96  105 102
Explorer  70 122 103 111 110 112 109
Lobo      80 134 113 121 125 119 120

'cause right now it looks like this:

Speed 20 88 75 80 68 77 81
Focus 30 90 77 83 71 84 85
Jetta 40 94 80 85 76 91 90
Contour 50 102 86 94 85 98 96
Tsuru 60 111 94 100 96 105 102
Explorer 70 122 103 111 110 112 109
Lobo 80 134 113 121 125 119 120

this is my code :

public class ApplicationArray {

    public static void main(String[] args) {
        int noise[][] = { { 20, 30, 40, 50, 60, 70, 80 },
                { 88, 90, 94, 102, 111, 122, 134 },
                { 75, 77, 80, 86, 94, 103, 113 },
                { 80, 83, 85, 94, 100, 111, 121 },
                { 68, 71, 76, 85, 96, 110, 125 },
                { 77, 84, 91, 98, 105, 112, 119 },
                { 81, 85, 90, 96, 102, 109, 120 } };
        String brand[] = { "Speed", "Focus", "Jetta", "Contour",
                "Tsuru", "Explorer", "Lobo" };
        for (int i = 0; i < noise.length; i++) {
            System.out.print("" + brand[i]);
            for (int j = 0; j < noise.length; j++) {
                System.out.print("" + " " + noise[j][i]);
            }
            System.out.print("\n");
        }
    }
}

thanks

Recommended Answers

All 4 Replies

Pad the Strings in the first column with spaces so that they are all the same length.
say you want the first column to be 20 characters wide and the String to print is 15 chars long, then you need to add a String of 5 spaces to fill the first column to 20.
Make a method that takes the String and the width of the column and returns a new String with the input String padded with spaces to the width number of characters.

thanks i did the first array

String brand[] = { "Speed    ", "Focus    ", "Jetta    ", "Contour  ",
                        "Tsuru    ", "Explorer ", "Lobo     " };

but with this one i don't get the part of making the method to pad it

int noise[][] = { { 20, 30, 40, 50, 60, 70, 80 },
                        { 88, 90, 94, 102, 111, 122, 134 },
                        { 75, 77, 80, 86, 94, 103, 113 },
                        { 80, 83, 85, 94, 100, 111, 121 },
                        { 68, 71, 76, 85, 96, 110, 125 },
                        { 77, 84, 91, 98, 105, 112, 119 },
                        { 81, 85, 90, 96, 102, 109, 120 } };

now my display looks like this:

Speed     20 88 75 80 68 77 81
Focus     30 90 77 83 71 84 85
Jetta     40 94 80 85 76 91 90
Contour   50 102 86 94 85 98 96
Tsuru     60 111 94 100 96 105 102
Explorer  70 122 103 111 110 112 109
Lobo      80 134 113 121 125 119 120

Look at the prinf() method. It has formatting Strings that should help.

Also the String class's format() method could be useful.

commented: thanks +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.