6 Replies - 278 Views - Last Post: 04 July 2013 - 12:33 PM Rate Topic: -----

#1 dragonman6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-June 13

Using String.format() with arrays

Posted 04 July 2013 - 10:17 AM

Hey guys. So im having a lot of trouble outputting these 3 arrays the way I want them. I have 3 different array, and i need to use string.format() to output these arrays.

Here is the code I have rightnow:
for ( int i = 0; i < tNumTeams; i++ )
{
        System.out.print(tArrayTeams[i]);
	System.out.println(String.format("%15s %15s", tWinArray[i], tLoseArray[i]));
}



And this is my output:
Lakers              0               0
Timberwolves              0               0
Nets              0               0



My 2 arrays on the right are getting messed up because the names of the teams are different lengths.

I want my output to look like this: (with the W and L)
                    W               L
Lakers              0               0
Timberwolves        0               0
Nets                0               0



I have a strong feeling it has something to do with the for loop. Any help would greatly be appreciated. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Using String.format() with arrays

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Using String.format() with arrays

Posted 04 July 2013 - 10:38 AM

Before you worry about the alignment, you need to fix the values: your display shows 3 per line - you only have 2 treated in your code
Was This Post Helpful? 0
  • +
  • -

#3 dragonman6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-June 13

Re: Using String.format() with arrays

Posted 04 July 2013 - 10:58 AM

I have treated 3 in my code. The teams get automatically printed on the very left side. I just need to align the 2 arrays on the right, without it getting mseed up by the length of the team names.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Using String.format() with arrays

Posted 04 July 2013 - 11:02 AM

Oh sorry - misread it. Your code would be more readable were you to include tArrayTeams in the format call. Looking again...

Try

System.out.printf("%-15s%6d%6d%n", tArrayTeams[i], tWinArray[i], tLoseArray[i]);


This post has been edited by g00se: 04 July 2013 - 11:10 AM
Reason for edit:: code

Was This Post Helpful? 0
  • +
  • -

#5 dragonman6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-June 13

Re: Using String.format() with arrays

Posted 04 July 2013 - 11:13 AM

From just constant trial and error, I just tested this following code out rightnow and it produced what im looking for, for the most part:

for ( int i = 0; i < tNumTeams; i++ )
{
	
    System.out.println(String.format("%-15s %10s %10s", tArrayTeams[i],  tWinArray[i], tLoseArray[i]));
											
}

And the output for that is:
[code]
Lakers                   0          0
Timberwolves             0          0
Nets                     0          0



But now I want to know, how can I get it so that the two arrays on the right are aligned exactly 2 spaces after the longest team name? and how would I add the W and L on top of each array?

For example, this output would be ideal:
              W          L
Lakers        0          0
Timberwolves  0          0
Nets          0          0


Was This Post Helpful? 0
  • +
  • -

#6 dragonman6  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 28-June 13

Re: Using String.format() with arrays

Posted 04 July 2013 - 11:19 AM

View Postg00se, on 04 July 2013 - 11:02 AM, said:

Oh sorry - misread it. Your code would be more readable were you to include tArrayTeams in the format call. Looking again...

Try

System.out.printf("%-15s%6d%6d%n", tArrayTeams[i], tWinArray[i], tLoseArray[i]);



I didnt see this before I wrote my last post. But your code works too. Do you know the answer to my latest post? Thanks for the help man
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Using String.format() with arrays

Posted 04 July 2013 - 12:33 PM

You'd need to scan the array for the longest string then build the format string dynamically

String formatStringStart = String.format("%%-%ds", longestString.length() + 2);


Effectively you'll be using String.format twice. The top header needs to be derived in a similar way.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1