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();
}
}
}
Maybe turn this:System.out.print(memoryArray[i][j] + " ");into this?System.out.printf("%5d | ", memoryArray[i][j]);
thats far better than it originally was, thanksis it possible to get horizontal lines between the rows?thanks
And often people would use + signs on corners.
http://en.wikipedia.org/wiki/ASCII_art
> And often people would use + signs on corners.We're into snazzy territory now, folks...
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
> 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.
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
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
Show us what you've done. It's not that hard to print out a '-', or even a whole bunch of '-''s.
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
If you want to draw a vertical line, why are you including "i" at all?
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.
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
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:-
clearly the difference is there are more of the latterare you suggesting i have "" in my code?
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.
i seeso where exactly in the outer loop does this go?
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.
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
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?
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
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?
yes i wanted them all on the same linethats my point it doesnt work!do u know why?
Because you printed the horizontal line and a newline between them and not after them!
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