0
package arrays;

import java.util.Iterator;

public class StringArray2D {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String row1[][]={{"Robert","Albert","Gilbert"},{"Fobert","Sobert","Nareg","Nari"},{"Marie","Sarie","Kharie","Aarie","Akiahsbdfuiah"}};
    display2(row1);
}

public static void display2(String x[][]){

    for (int row = 0; row < x.length; row++) {
        for (int column = 0; column < x[row].length; column++) {
            System.out.print(x[row][column] +"\t"); 
        }


        System.out.println();
    }

}
public static void display(String x[][]){
    for (int row = 0; row < x.length; row++) {
        for (int column = 0; column < x[row].length; column++) {
            System.out.println(x[row][column] + "\t");
        }
        System.out.println();
    }

}

}

hello guys ... im trying to display this 2D array properly Proper Display

but instead i get the display like this Improper Display

the method "display2" works properly , the method "display" doesnt work properly what am i missing ?

3
  • Do you know what the ln in System.out.println means? and how is it different from System.out.print? Commented Jul 13, 2017 at 6:31
  • display() is using System.out.println which prints to a newline each time. Your code is fine. Commented Jul 13, 2017 at 6:31
  • 1
    annnn yess i got it ... didnt pay attention ... thats it ... should be System.out.print instead of System.out.println Commented Jul 13, 2017 at 6:32

1 Answer 1

1

Then use the "display2" method, what's the problem? This is the difference: System.out.print(x[row][column] +"\t");

vs

System.out.println(x[row][column] +"\t");

The first one writes what you provide, the second one writes it and adds a new line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.