• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print in columns?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to create a project that will simulate a lottery drawing with random tickets and random winners drawn. So far I have 2 constructors that generate the tickets and draw the potential winners. I'm trying now to print them but want it to have headings and lines and be in 5 columns. How would I do that? Here's what I have so far. I'm fairly new at java just need some guidance.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

want it to have headings and lines and be in 5 columns.


There are a couple of ways to put printed output in columns:
use tab characters before the data for each column
use format strings with the printf method.  See the API doc for the Formatter class for how to build format strings
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are the 5 columns for five different data items, so that you have one winner per line? Or do you want Winner #1 through Winner #5 on the first line and Winner #6 through Winner #10 on the second line and so on? Or do you want the first 1/5 of the winners in column 1, the next 1/5 of the winners in column 2, and so on?
 
Rancher
Posts: 285
14
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw something the other day saying \t is for windows only, and its recommended to use System.seperator or something like that, for cross platform.
 
Steven Villarreal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Paul, I would like to have the number of potential winners i typed in so for instance if i typed in 100 then i would want 5 columns with the 100 potential winning tickets to be in sort of like this
123   124   411    543   523
541   765   869    213   433
432 ... and so on .
 
Steven Villarreal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but at the top it will say


Potential Winners
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay... so, ordered like this?

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

or like this?

1 5  9  13 17
2 6 10 14 18
3 7 11 15 19
4 8 12 16 20
 
Steven Villarreal
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well its random numbers that are drawn so if i type in 300 and i want 100 selected numbers it'll print out 100 random numbers from 0 to 299. but yes probably the second one
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they are random numbers then it doesn't matter what order they get output in, right? In that case I'd recommend the first option because it's a lot (a LOT) easier to produce. Just write out five of them with suitable spacing, go to the next line, write out five more, go to the next line, and so on.

The "suitable spacing" is the tricky part.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic