printing 2D arrays

J

JS

Hi all, Im just learning Java and was wondering if there is any way of
displaying a String[][] in a grid form. I have managed it using loads of
System.out.println statements printing each row at a time but i need to be
able to print it in a text area or similar, as long as its in a 15 x 15
square grid i dont mind how. Any ideas welcome. thanks in advance
James
 
D

dado0583

I'm guessing your implementing the String[] [] into a GUI output. You
can simply create a new instance of a JTable passing it the array. If
you don't like the look you can change it using the methods of the
JTable class.

Alternatively you can simply concatenate all of the strings into one
string. Create a temporary string and then concatenate each string from
the array into the temp one, including all the formatting you have
already done. Then create a JTextArea, passing it the temp string.
Hope this helps
 
O

omar

strArray[][] has your data.

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

omar
 
J

JS

Thanks for your help but how do i specify which component it goes into. its
for a GUI output that i need it and at the moment i have a TextArea but cant
print multiple lines with the right formatting (the right format being a 15
x 15 square grid). how do i say "print to that area" so to speak?
 
J

JS

PS I forgot to say, that I did try System.out.println("...../n.........");
but no luck, it just printed /n in the line
JS said:
Thanks for your help but how do i specify which component it goes into. its
for a GUI output that i need it and at the moment i have a TextArea but cant
print multiple lines with the right formatting (the right format being a 15
x 15 square grid). how do i say "print to that area" so to speak?
omar said:
strArray[][] has your data.

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

omar

 
K

kjc

JS said:
Thanks for your help but how do i specify which component it goes into. its
for a GUI output that i need it and at the moment i have a TextArea but cant
print multiple lines with the right formatting (the right format being a 15
x 15 square grid). how do i say "print to that area" so to speak?
strArray[][] has your data.

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

omar


If this is a GUI based effort, use a JTable.
 
J

JS

Thanks for your help everyone, done now.
kjc said:
JS said:
Thanks for your help but how do i specify which component it goes into. its
for a GUI output that i need it and at the moment i have a TextArea but cant
print multiple lines with the right formatting (the right format being a 15
x 15 square grid). how do i say "print to that area" so to speak?
strArray[][] has your data.

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

omar


If this is a GUI based effort, use a JTable.
 
Joined
Oct 14, 2009
Messages
2
Reaction score
0
2d array print

i think if u can print it like this
int[][] s={{1,2},{2,3},{4,5}};

for(int[] a:s)
System.out.println(Arrays.toString(a);

or just
System.out.println(Arrays.deeptoString(s);

but in the second case u wont get it as grid.

JS said:
Hi all, Im just learning Java and was wondering if there is any way of
displaying a String[][] in a grid form. I have managed it using loads of
System.out.println statements printing each row at a time but i need to be
able to print it in a text area or similar, as long as its in a 15 x 15
square grid i dont mind how. Any ideas welcome. thanks in advance
James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top