• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looped spacing  RSS feed

 
Aidan Murphy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have been trying to space out output on a Java console window so that I have three columns with 6 rows of data from three different arrays. The code I have so far outputs the data with no problem however the spacing between the columns is uneven and wanted to know if anyone could help me with it. My loop so far is made up as follows



As you can see I have been manually adding the space between each element of the array but this means that the space between each element is different because the size of each element is different if someone could help me work out a loop that works out an even amount of space between the elements and then print this along with the elements that would be great, I am a beginner and I just want to practise my loops for spacing so any help would be great full thanks
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using System.out.printf() instead, you can control the output format much better with it. printf Javadocs
 
Aidan Murphy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Try using System.out.printf() instead, you can control the output format much better with it. printf Javadocs


hi thanks for getting back to me, i have never used System.out.printf() before so not sure how it works i will read into it and see what i can work out, however i really wanted to use classes that i have used before as i'm just starting out so if anyone knows how to do with System.out.println() that would be great
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree; printf is really useful. You can read about the % tags in the Formatter class, but don't spend too long there. There are thousands of possible combinations. There are two sections in the Java Tutorials which will probably help: 1 2. The bits about formatting are probably most useful at present.
 
Aidan Murphy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys did some reading up on printf and it does exactly what i need it to do
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aidan Murphy wrote:hi thanks for getting back to me, i have never used System.out.printf() before so not sure how it works i will read into it and see what i can work out, however i really wanted to use classes that i have used before as i'm just starting out so if anyone knows how to do with System.out.println() that would be great

The trouble is that it's going to be much more difficult to do with println() alone, because it doesn't have any "goodies" to help you out with this sort of stuff; printf() (or String.format(), which uses the same syntax) on the other hand, were designed for precisely this sort of thing.

Very quick starter for you:

printf() takes a "format" string followed by any number of objects - ie, the things you want displayed.

The format string contains "placeholders" (or "fields") of the form:
  "%[n]x"
where:
  • "%" denotes the start of a field. Because of this, if you actually want to see a '%' sign, you have to use two in a row, ie: "%%".
  • "n" is an optional "width" specification (hence the square brackets).
    The only wrinkle with this is that printf() normally right-justifies its output; so if you want it left-justified, you have to add a minus sign ("-") at the front, making it look like a negative number.
  • "x" is a "type" qualifier. In your case, I suspect it will be "s" since the things you're displaying are Strings.

  • So, let's say you want to allow 30 spaces for the name, 20 for the votes, and whatever's left for the percentage. The statement would then look something like:

      System.out.printf("%-30s %-20s %s%%", printNames[i], printVotes[i], printPrecent[i]);

    where:
  • "%-30s" denotes a field of 30 characters, left-justified, that takes a String. And because it's the first field in the format string, it will use the first argument after the format string - ie, printNames[i].
  • "%-20s" denotes a field of 20 characters, left-justified, that takes a String, and it will use the second argument (printVotes[i]).
  • "%s" has no "width", so it will simply print out its argument (printPrecent[i]) as is.
  • "%%" will print out a single '%' sign.

  • Hope it helps. There's other stuff to know, so I suggest you look at the links Junilu and Campbell gave you; but it's really quite straightforward once you get used to it.
    And believe me: it's a LOT simpler than trying to keep track of all those widths yourself.

    BTW: Shouldn't it be printPercent[i]?

    Winston

    [Edit] Ooof. Waaay too slow. I'll leave it there for anyone else who might find it useful.
     
    Aidan Murphy
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    Aidan Murphy wrote:hi thanks for getting back to me, i have never used System.out.printf() before so not sure how it works i will read into it and see what i can work out, however i really wanted to use classes that i have used before as i'm just starting out so if anyone knows how to do with System.out.println() that would be great

    The trouble is that it's going to be much more difficult to do with println() alone, because it doesn't have any "goodies" to help you out with this sort of stuff; printf() (or String.format(), which uses the same syntax) on the other hand, were designed for precisely this sort of thing.

    Very quick starter for you:

    printf() takes a "format" string followed by any number of objects - ie, the things you want displayed.

    The format string contains "placeholders" (or "fields") of the form:
      "%[n]x"
    where:
  • "%" denotes the start of a field. Because of this, if you actually want to see a '%' sign, you have to use two in a row, ie: "%%".
  • "n" is an optional "width" specification (hence the square brackets).
    The only wrinkle with this is that printf() normally right-justifies its output; so if you want it left-justified, you have to add a minus sign ("-") at the front, making it look like a negative number.
  • "x" is a "type" qualifier. In your case, I suspect it will be "s" since the things you're displaying are Strings.

  • So, let's say you want to allow 30 spaces for the name, 20 for the votes, and whatever's left for the percentage. The statement would then look something like:

      System.out.printf("%-30s %-20s %s%%", printNames[i], printVotes[i], printPrecent[i]);

    where:
  • "%-30s" denotes a field of 30 characters, left-justified, that takes a String. And because it's the first field in the format string, it will use the first argument after the format string - ie, printNames[i].
  • "%-20s" denotes a field of 20 characters, left-justified, that takes a String, and it will use the second argument (printVotes[i]).
  • "%s" has no "width", so it will simply print out its argument (printPrecent[i]) as is.
  • "%%" will print out a single '%' sign.

  • Hope it helps. There's other stuff to know, so I suggest you look at the links Junilu and Campbell gave you; but it's really quite straightforward once you get used to it.
    And believe me: it's a LOT simpler than trying to keep track of all those widths yourself.

    BTW: Shouldn't it be printPercent[i]?

    Winston

    [Edit] Ooof. Waaay too slow. I'll leave it there for anyone else who might find it useful.


    hey Winston even though you were slow to get in there i have to say your detailed explanation was great and you found some spelling mistakes so thanks lol
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Aidan Murphy wrote:hey Winston even though you were slow to get in there i have to say your detailed explanation was great and you found some spelling mistakes so thanks lol

    You're welcome. Just for the future: when you're replying, you don't have to quote the entire post. Just include enough to make it clear who you're replying to. Saves us from having to abuse the scroll button.

    Winston
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!