Please help, I have only been using Java 2 weeks. I am to ask the user how many columns he wants and then print an ascii table in that many columns. So far I can get the program to print the ascii table in one column. How do I specify the number of columns I want?



Oh, dear.
No, I'm afraid this is a Java forum. You know, Java, the programming language? The one that isn't HTML, the markup language?

Oh, never mind...

commented: Does not contribute to thread. +0
commented: sometimes they do ... sometimes they don't :) +6

Set the number of columns to c.

Create a for loop (or while) to go from x to y, being the range of ascii to print. Inside the loop, have a counter to go from 1 to c. Print each character out and then increment your counter. When the counter = c, newline and reset the counter. That should take care of things.

Hope this helps.

Oh, my goodness. Oh, my stars and garters.
This thread is really pulling them out of the woodwork, isn't it?

What will they think of next? "To print in columns, use a 3-d printer to make tall round things intended to hold up buildings. Then, load some sticker stock into a standard printer and print whatever it is you want printed on the sticker stock, and stick it on the tall round things intended to hold up buildings. Voila, printing on columns. For best results, don't make your tall round things fluted, or the sticker stock won't stick very well."

Come, on, brave boys, try again. Let's see what else you can come up with to answer this question. Two points of rep to the best answer, and another two to the worst. Let's have 'em!

What is your solution then Jon? I thought it was a viable one, but I guess not.

Your solution prints a string in blocks of c characters, disregarding whitespace.
Easier way to do that would look something like this:

for (int i = 0; i<str.length()/c; i++)
  System.out.println(str.substring((i*c), ((i+1)*c)-1))

but I suppose there's probably an easier way still - I'd have to wonder if column width for system output is a settable property - but this is at least easier.

But that's not what the poster asked for, is it?

S/he wants to print out data in tabular form.
printf and \t will do that. Making some assumptions about the way data is stored, it would look something like:

//print header:
System.out.printf("%s\t%s\t .... \n", headers[1], headers[2], [...]);

//loop through a 2d array and print it in columns
for (int i = 0; i < data.length; i++)
    System.out.printf("%s\t%s\t .... \n", data[i][1], data[i][2], [...]);

If you want to be all dynamic and stuff, you could put an internal loop over data.length and use print() instead of printf - depends on whether you know what's in your data array.

Assuming here that everything is about the same length. If you're totally blind about what's in your data array - that is, if you're making this pretty flexible - you could correct for that, too. Or if you know, for example, that it's all doubles, you could use the usual printf formatting.

But really, it's all just printf and \t, just like you'd do in C.