3

My code is this:

// for loop for displaying multiple values
for (int index = 0; index < 5; index++) { 

    System.out.println("\t\t" + name[index] + "\t\t\t" + "$" + sales[index] + "\t\t\t" + "$" + comm[index] + " ");
}

This is the current output, but I want to display output with same spacing

             Sales and Commission
    ======================================================
     Salesperson        Sales Amount        Commission
    -----------------------------------------------------
    u ujuhygtfd         $89000          $8900.0 
    uh uh           $87000          $8700.0 
    t t         $65000          $5200.0 
    f f         $54000          $4320.0 
    u r         $43000          $2580.0 

     Total: 9 Data entries

How can I display my data like this?

             Sales and Commission
    ======================================================
     Salesperson        Sales Amount        Commission
    -----------------------------------------------------
    u ujuhygtfd         $89000          $8900.0 
    uh uh               $87000          $8700.0 
    t t                 $65000          $5200.0 
    f f                 $54000          $4320.0 
    u r                 $43000          $2580.0 

     Total: 9 Data entries 
chancea
  • 5,858
  • 3
  • 29
  • 39
  • It usually is helpful to add a language tag. I assume java in this case? – chancea May 12 '15 at 15:14
  • I don't if there is a better way (probably there is), but you can test on the size of your strings, for example all the `Salesperson`'s `strings` must be 10 characters long, so if your string is only 4 you'll add 6 `spaces` – Othman Benchekroun May 12 '15 at 15:18
  • @njzk2: sure, but is it an exact duplicate? see also http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – serv-inc May 12 '15 at 15:28
  • Instead use some GUI components from Swing/AWT, if redirecting the output to the I/O console is supplementary. – Tiny May 12 '15 at 15:34
  • @user1587329: In this case, I am fairly confident that the information contained in the duplicate candidate is largely sufficient and there is no need for anything else (apart from may be writing their complete program for the OP, but that's beyond the scope, i should say) – njzk2 May 12 '15 at 16:15
  • Maybe not exactly what you're looking for, but related: http://stackoverflow.com/a/29054406/3182664 – Marco13 May 13 '15 at 11:44

2 Answers2

2

As said in this answer (slightly modified):

Use System.out.format. You can set lengths of fields like this:

System.out.format("%32s%10d%16d", name[index], sales[index], comm[index]);

This pads name[index], sales[index], and comm[index] to 32, 10, and 16 characters, respectively.

See the Javadocs for java.util.Formatter for more information on the syntax (System.out.format uses a Formatter internally).

Community
  • 1
  • 1
serv-inc
  • 35,772
  • 9
  • 166
  • 188
  • 1
    An additional benefit of formatted Strings is they are easier to read usually. `("Name: " + name + "Commission: " + amount)` versus `("Name: %s Commission: %d", name, amount)` and it's hard to shoot yourself in the foot and forget a space, the first String in my example could read `"Name: CaptainManCommission: 100"`, it's hard to immediately tell though. – Captain Man May 12 '15 at 15:39
0

You could do something like

    for (int index = 0; index < 5; index++) { // for loop for displaying multiple values
        int numOfSpaces = 20 - name[index].length();
        String spaces = "";
        for (int i = 0; i<numOfSpaces; i++)
            spaces += " ";
        System.out.println("\t\t" + name[index] + spaces + "$" + sales[index] + "\t\t\t" + "$" + comm[index] + " ");
    }

And change the arbitrary 20 to anything you want, eg. the longest string in names + 5.

Ryan
  • 2,058
  • 1
  • 15
  • 29