grid lines?

Hi,

i have a class with contains a 2d array and prints out the information in a table

what i was wondering was, is it possible to print grid lines around each bit of information like a table

the code for the class is provided below

any help or pseudo code is much appreciated

thanks

import java.util.Random;

publicclass Memory

{

publicstaticvoid main(String args[])

{

int[][] memoryArray=newint[50][8];

Random randnum =new Random();

for(int i=0;i<50;i++)

{

for(int j=0;j<8;j++)

{

memoryArray[i][j]=randnum.nextInt(1023);

}

}

for(int i=0;i<50;i++)

{

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

for(int j=0;j<8;j++)

{

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

}

System.out.println();

}

}

}

[1860 byte] By [johnnysmitha] at [2007-11-27 3:53:48]
# 1
Maybe turn this:System.out.print(memoryArray[i][j] + " ");into this?System.out.printf("%5d | ", memoryArray[i][j]);
paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 2
thats far better than it originally was, thanksis it possible to get horizontal lines between the rows?thanks
johnnysmitha at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 3
Hint: "-"
DrLaszloJamfa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 4
And often people would use + signs on corners.
paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 5
http://en.wikipedia.org/wiki/ASCII_art
paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 6
> And often people would use + signs on corners.We're into snazzy territory now, folks...
DrLaszloJamfa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 7
hmm thanks for the hintiv thrown it in everywhere in the line of codei can get it to print out 1 - after every value but its not a grid line between the valuesare you prepared to offer anymore hints or the solution?thank you again for the help
johnnysmitha at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 8
> are you prepared to offer anymore hints or the solution?I was hoping to leave it as an exercise. It doesn't seem that hard to do.
DrLaszloJamfa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 9

You have two nested loops, right? The outer one prints rows, and the inner one prints items in each row (columns).

If you want to print a horizontal line, then, you'd do that within the outer loop, but outside of the inner loop.

Where you're outputting the empty newline now.

Message was edited by:

paulcw

paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 10
argh, im having a nightmare herei know what you mean by putting in the outer loop. thats helps - thanks.however, i still know know where to place the "-" into the code?iv tried everywhereiv tried the printf thing on the outer loop also
johnnysmitha at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 11
Show us what you've done. It's not that hard to print out a '-', or even a whole bunch of '-''s.
paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 12

well what iv done has been mostly focused around this line like you said

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

iv attempted the following

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

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

System.out.printf(" %5d -["+i+"] ");

System.out.printf(" - ["+i+"] ");

and probably more but i cant grasp it

johnnysmitha at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 13
If you want to draw a vertical line, why are you including "i" at all?
paulcwa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 14

You've got the wrong idea there - it's not the addition of a single "-" character that you're looking for. Instead, think about what the results of your print statements will be, and how to print (separately from the one you have already) a row of repeated - characters after printing a row of values.

DavidKNa at 2007-7-12 8:57:53 > top of Java-index,Java Essentials,New To Java...
# 15

all i wanted at the start was to print my 2d array in a table

i.e vertical and horizontal lines surrounding all the values

the | character between the values presented as close to a vertical line as i thought possible then i wanted a similar horizontal approach between the lines

it isnt at all relevant to my project, i just wanted something to look at bit on the fancy side

johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 16
Yeah.So anyway, you want to draw a horizontal line. What's the problem exactly? And why are you outputting "i"?Think about it: what's the difference between this:-and this:-
paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 17
clearly the difference is there are more of the latterare you suggesting i have "" in my code?
johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 18

If you want "" then you can either print "-" or you can print "-" multiple times. Or you can even print "-" multiple times. Say, if you have X elements, and each element takes 5 spaces to print, then for each X you could print "--" beneath it, and get a line of the right length.

Note that if you want "", then you don't actually want "-1-2-3-4-5-6-7-8", which is entirely different.

paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 19
i seeso where exactly in the outer loop does this go?
johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 20
I told you already.Look: you print a row's worth of data.Then you print a newline.Then if you want to print a horizontal line, print it after the newline.Then print another newline.
paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 21

ah thats alot clearer

i feel as though im getting closer with this:

for(int i=0;i<50;i++)

{

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

System.out.println();

System.out.println("--");

System.out.println();

where am i going wrong?

thanks

johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 22
It looks OK to me. You're getting your horizontal line, right?You've stopped printing the column data, for some reason. Was that just to keep your code simple, or did you actually take it out?
paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 23

theres definately something not right because its printing like this

787 | 207 |79 | 161 | 368 | 220 |98 | 140 |

[27] --

the full code is:

for(int i=0;i<50;i++)

{

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

System.out.print("--");

System.out.println();

for(int j=0;j<8;j++)

{

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

System.out.printf("%5d |", memoryArray[i][j]);

}

System.out.println();

}

theres definitely something not right here

can u see what?

thanks

johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 24

Well, personally, I'd create a third for loop, after the inner one.

You'd notice that this:

System.out.printf("%5d |", memoryArray[i][j]);

prints 7 characters at a time (as long as the number has fewer than 6 digits).

So you could loop, and for every time you printed the above, print this:

System.out.print("-");

Also, why are you printing the line between the "[27]" and the data on the line? Didn't you want it all on the same line?

paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 25
yes i wanted them all on the same linethats my point it doesnt work!do u know why?
johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 26
Because you printed the horizontal line and a newline between them and not after them!
paulcwa at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...
# 27
ahh, iv got it sortedits doing what i want it to dothanks for all your helpid donate you a duke star but you've got plenty of them :pcheers again mate
johnnysmitha at 2007-7-21 20:58:28 > top of Java-index,Java Essentials,New To Java...