Chapter 2: Using Printf
In this video note, let’s talk about printf. You’ve already been using println, so let’s see where we have some limitations in terms of formatting the kinds of output that you might want. So, for example, consider this table over here. Let’s say that you’d like to output this table where you’ve got an item name, and here are some made-up items, from fizzbrobs to frotuttis. And we want to have a total in the next column, and then a quantity, and then a price. And so we just take the quantity, multiply by the price. That gives us the total. And here is some code that might do this. So we just have a variable for the number of fizzbrobs, the number of foobins, and the number of frotuttis, and then we have the price for each one. And then notice down here, for frotuttis, the price is $2.125. So it goes beyond the second decimal point. So if you had a whole bunch of frotuttis, you know, this .005 would add up.
And to print out the table, if all you knew is println, then you might use something like this, where you use println, you print out the name, total quantity, and price. Print out fizzbrob—this would be trying to generate this first row to fizzbrob. Plus here you multiple the number times the price, and then here are the number of fizzbrobs, which would go here. And then the price, which should go up here. And if we have the right number of spaces in here, then everything will line up.
So let’s see what this will look like. And so here I’m—I’ve got this little program entered in NetBeans already, where I’ve got the numbers over here, the price over here, and then here is a whole bunch of println statements. So, what happens if we run it? Then this is our output. It doesn’t quite line up right. Here, we notice that the fizzbrobs line up fine. And then, with the number of spaces we’ve added, it looks great for fizzbrob. But for foobin, it’s doesn’t quite line up right. Well, we made it look right for the name and total, but this column is off a little bit. And then this one’s off a little bit as well. And then frotutti, it had another character in here, and it’s off a little bit over here. It’s also got this 6.375, which maybe you don’t want. Maybe you’d like to round that up to 6.38 since normally we don’t have fractions of a penny that we want to output. And so we could make this work by changing the number of spaces in here. But any time you change the items, then it’s not going to line up correctly. And so here’s where we can use printf to try to output these things in formatted order.
So with printf, we have this new thing: that System.out.printf. And if you’ve ever used C, than this works the same way as printf in the C programming language. So the idea is, you give it some string and then inside this string, you give it whatever text you want. You could just put .hello or something. And if we did this just by itself, this would print out “hello.” Printf doesn’t add a new line, and so if you wanted a new line at the end, you have to add it. So we could just do regular print with printf. But where this is useful—where the real value comes in—is you can put these things inside here that are format specifiers. And the format specifiers are like placeholders for variables. So if you want to put a floating point number in here, then you could put—so you could say something like hi %f and then a comma. And then whatever number you stick in here, like 3.1, then this 3.1 is going to be filled in for the %f. And the output would be “hi 3.1.” So—and you can put more than just one format specifier in here, you could put as many of them as you’d like. So, for example, if I put in %d and then a space and then %f, then the first parameter I put, say as 10, and the second is 3.1, then the 10 is going to go fill in the slot for %d, and that’s for a decimal integer number. And then the 3.1 is going to fill in for the %f, and that’s for a floating point number. And so this would output 10 3.1.
Now where this is really handy if you want to specify how many characters should go into this field. So if I did something like %5d and then gave it 1, then it’s going to output 1, but it’s going to use 5 characters for that 1. And so that’s what this is saying here. You have a field of 5 spaces. And so it will give you 5 spaces with the 1 in the rightmost space. Another really useful one is you can use %, say 3.2f, and you give it a floating point number, like 1.121, then this is—this gives you a field of—let’s use a bigger number than 3. Let’s use 5. This gives you a field of 5 spaces—so 1, 2, 3, 4, 5. And then it’s going to print the number but only give you 2 values after the decimal point. And so this would print out 1.12, and you’d have 1 space over here on the left side for a total of 5 spaces. If I had given the 6, then I’d have a field of 6 spaces.
So here we have various format specifiers to print out a character, a decimal, a float, an exponential, or a string. So we can use this idea in our program, and the idea is as shown over here. Let’s give it, say, a space of 10 spaces for the item name, 10 spaces for the total. So if I kind of line these up, item name’s going to take up 10 spaces. Total will take up 10 spaces. It will match with this one. Quantity is going to take up 10 spaces. And then the same thing for unit price; that’s also going to take up 10 spaces. And these are all specified as strings. So this will print out—so I go back up here—and the idea is that will print out the first line of my chart. And this will print out the three rows below it. And so this will print fizzbrob. But I’ll go in here for the first %f—let’s do it like this. And then here I’ve got the number of fizzbrobs time the price. And let’s print that out as a floating point number with two decimal places. And then it’s going to take up 10 spaces. And this over here will be the number of fizzbrobs. And the last one is a price. The price will also be in 10 spaces. And then give us 3 numbers—3 digits after the decimal point. And then that’s just repeated for all the other items.
So let’s go back to our code and use this new version. And so, instead of System.out.println, we’ll use System.out.printf. And the string is going to be %10s %10s, and then the first one is the item name. And then the total. And then quantity. And the last one here is unit price. So here, I’ve got my field of 10 spaces for each one of these, and then here is the first guy that goes in right here. Second one goes in there, and so forth. Then we can repeat this for the next ones, for fizzbrob. My format specifier looked like this; it’s %10s for the fizzbrob. And then we had %10.2f for the total, and then %10d for the quantity, %10.3f for the unit price. So fizzbrob will come in here for this first %10s. After that I’ve got—let me just delete all this—after that I’ve got number of fizzbrobs time the price of the fizzbrob. After that I’ve got the number of fizzbrobs, so the number is going to go in for this third slot. And the last one is the price of the fizzbrob. And then this part goes away.
So there is my four parameters that get filled in for these four placeholders here. And then I just need to repeat that for the other two, for foobins and frotuttis. So I’m going to copy that and paste it. And then over here, I’ll change this guy to a foobin. And then the price of the foobin. And this last one becomes a frotutti. So here, I’ll just copy and paste over these, and I ca get rid of the old printlns.
Alright. So there’s our new version that uses printf instead of println. Oh, and there’s one more thing I need. Here, I’ll run it and show you what’s going to happen. I run it, and everything’s all in one line. That’s because I didn’t add a new line in here. So a need to add a \n. So now this will output on a different line. And so here’s my table where everything is nicely output. And it’s all nicely lined up where I’ve got 10 spaces for each one of these things. And the default, as you can see, that is right-justified for each one of these columns. If you want a left justification instead, you can put a minus sign in front of the number. So, like %-10. And I’ll go do that for all of these, so that will make all of these left-justified. Oh, the other thing I should pint out—once I finish putting in all the minus signs—is notice here, for frotutti, it’s only got two digits after the decimal point—6.38—and so it’s rounded it for us.
So let’s see how this looks with the minus signs. So we get the same values, but now everything is left-justified instead of right-justified inside the field of the 10 spaces for each value. So there’s a little bit about the nice benefits that we get from printf. Anytime you need formatted output, then printf is a really handy way to go although it does take a little while to figure out what all of these format specifiers are and the different ways you can format the fields. And here we’ve just given some basics. There are many other formatting options that you can look up in your reference. You can also use—and there’s a version of printf that’s sprintf that prints to a string. So—and you can also use the same version when we talk about file I/O later on, about outputting to a file. So the idea of formatted output appears in other places in the Java programming language.