Skip to main content
GameDev.net gamedev.net
🔒 Locked

Format String Method in Java?

Started by samuraichikx Oct 4, 2010 at 10:58 PM 1 replies 1.7k views
Original Post
samuraichikx
samuraichikx
I just finished writing my first programming project of the quarter for my ICS class. My professor had us format the output using a String class method in java known as .format(). The TA helped me and my partner out but we're still lost about how exactly this works. Can someone explain this too me, please?

This is the example he used:
------------------------------------------------------------------------------

The first parameter you pass to it is called a format string, which is used to tell it how you'd like the data to be formatted, with placeholders for the data. Subsequent parameters specify the data that will replace the placeholders. Here's an example from a hypothetical Person class:

String s = "";
s += String.format("%-10s: %s\n", "Name", name);
s += String.format("%-10s: %s\n", "Title", title);
s += String.format("%-10s: %4d\n", "Height", height);
s += String.format("%-10s: %4d\n", "Weight", weight);
System.out.println(s);
-------------------------------------------------------------------------------
Thank you!
dalindeck
dalindeck
From what I can see (I've never used Java), the .format() is very similar to the printf() function in C, I'll break it down the best I can.

Basically, the .format() command allows you to modify the string, for example:

s += String.format("Bob Joe\n");

Will write out "Bob Joe" followed by a newline.
What you see in there is formatting/variable specifiers (not the technical term, but you understand. The first parts are dealing with strings, so we have:

"%-10s: %s\n", "Name", name

The %s will display whatever is after the commas. It's hard to explain, but if you had:

"%s: %s\n", "Name", name

Then it would display:

"Name : "

The -10 between the % and the s specifies a formatting flag. I believe this is telling it that it wants it left aligned and padded with 10 characters, so instead of having:

"Name : "

You have:
"Name      :     <name>"


That covers all of the strings, now to the spot that has the "%4d\n"

This is essentially the same thing, however since height is what I am assuming to be a double, then you are using %d to specify the variable type to display (so that the stream knows what to do with it).

The 4 in between the %d is telling it that you want 4 spaces before displaying the number, so the output would look like this:

"Height    :    <height>"


Same goes for weight.

I hope this helps! And I hope I was correct in associating the .format() function with the printf() function :D
Jacob Foster
Software Engineering student at the Oregon Institute of Technology
Zahlman
Zahlman
Quote:
Original post by samuraichikx
we're still lost about how exactly this works.


Let's read over the description step by step.

Quote:
The first parameter you pass to it is called a format string


OK, so let's look at one of the calls to String.format used in the example:

String.format("%-10s: %s\n", "Name", name)


There are three parameters here: "%-10s: %s\n", "Name", and name.

The first one is "%-10s: %s\n", and that is our format string.

Quote:
which is used to tell it how you'd like the data to be formatted,


I should hope that much is self-explanatory.

Quote:
with placeholders for the data.


The contents of the format string are like a template that get filled in with more data. The "placeholders" are the parts of the string starting at a '%' sign and ending with a character describing a data type. Here, 's' describes a "string". We have two placeholders: '%-10s' and '%s'. Each of these will be replaced with a string.

Quote:
Subsequent parameters specify the data that will replace the placeholders.


So, the strings that replace the placeholders come from the other two parameters. We will substitute in "Name" for the '%-10s' placeholder, and name (i.e., whatever is in that variable) for the '%s' placeholder.

The rest of the string - a colon, a space and a newline - just gets copied verbatim.

The '-10' in '%-10s' gives additional formatting information. It says to pad this string (with spaces) to at least 10 characters, with [edit after reading the documentation] left-justification (i.e., the spaces come after the actual "Name" text).

The net effect, in the example, is to make all the colons line up in the output, because each of the strings "Name", "Title", "Height" and "Weight" is being padded to the same length.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.