01 13 Formatting with Printf

Professor Kulla
13 Jul 201916:20

Summary

TLDRThis educational video script introduces the 'printf' method in Java, which is used for formatted output. It explains the use of special characters like %s for strings, %d for integers, and %c for characters, with a focus on %f for floating-point numbers, particularly for displaying monetary values with two decimal places using %.2f. The script also demonstrates how to align text in columns using printf, emphasizing the importance of spacing and formatting for clear and organized output. It concludes with tips on using printf for common programming tasks, such as printing integers and characters, and the necessity of including a newline character for proper display.

Takeaways

  • 📝 **Printf Introduction**: The video introduces printf as a method for formatting output in Java, similar to print and println but with enhanced formatting capabilities.
  • 🔡 **Special Characters in Printf**: It explains the use of special characters like %s for strings, %d for integers, %c for characters, and %f for floating-point numbers in printf statements.
  • 💵 **Formatting Money**: One of the primary uses of printf is to format money, ensuring that floating-point numbers display two decimal places, which is crucial for financial representations.
  • 🔢 **Floating-Point Formatting**: The script demonstrates how to use %f and %.2f to control the number of decimal places displayed, with %.2f being particularly useful for money formatting.
  • 📊 **Column Alignment**: Printf is used to align text in columns, which is helpful for creating tables or lining up data in a readable format.
  • 🔠 **String Padding**: The video shows how to use printf to pad strings with spaces to ensure they occupy a specific number of characters, enhancing the visual structure of the output.
  • 🔄 **Left and Right Alignment**: It discusses how to adjust the alignment of printed values within their designated spaces, either to the left or right, using negative or positive numbers in the printf format.
  • ⏏️ **New Line Character**: The script points out that printf does not automatically add a new line, and %n must be used if a new line is desired after the printf statement.
  • 🔧 **Eclipse Assistance**: The video mentions that the Eclipse IDE can assist in writing printf statements by autocompleting parts of the code, which can be both helpful and sometimes intrusive.
  • 🔑 **Common Printf Uses**: The key takeaways highlight that %f is frequently used for monetary values, %s for columnar data, and %d for integer values, making these the most common formatting specifiers for beginners.

Q & A

  • What is printf and how does it differ from print and println in Java?

    -Printf is a method used in Java for formatted output, similar to print and println, but it allows for better control over the formatting of the output. Unlike print and println, which print raw output and automatically add a newline respectively, printf requires specific formatting characters to control the output's appearance.

  • What are the special characters used in printf for formatting?

    -Special characters used in printf for formatting include %s for strings, %d for integers, %c for characters, and %f for floating-point numbers. These characters are placeholders that tell printf how to format and display the variables that follow in the method call.

  • How can printf be used to display currency with two decimal places?

    -Printf can be used to display currency with two decimal places by using the %f formatting character followed by '.2', which specifies that two decimal places should be shown. This ensures that the output is formatted to display money correctly, such as 1.70 instead of 1.7.

  • Why is it necessary to format floating-point numbers when displaying currency?

    -Floating-point numbers should be formatted when displaying currency because financial transactions typically require two decimal places for cents. Java's default double representation might not show trailing zeros, which can lead to incorrect representations of currency amounts.

  • How does printf handle the alignment of text in columns?

    -Printf handles the alignment of text in columns by using the %s formatting character followed by a number that specifies the width of the column. The text is then right-aligned within that width by default. To left-align the text, a negative sign is placed before the width number.

  • What is the purpose of using a newline character in printf?

    -The newline character, represented by ' ', is used in printf to create a new line after the formatted output. Since printf does not automatically add a newline like println, it is necessary to include this character if a new line is desired after the output.

  • Can you provide an example of how to use printf to display a variable with a specific width?

    -Yes, to display a variable with a specific width in printf, you would use the formatting character for the variable type followed by the desired width. For example, '%-15s' would left-align a string in a 15-character wide field.

  • How does printf handle spaces in the formatted output?

    -Printf handles spaces in the formatted output by padding the remaining space in the specified width with spaces. For example, if a string is shorter than the specified width, printf will add spaces to the right (or left, if negative width is specified) to fill the field.

  • What is the difference between using printf and print or println for displaying columns of data?

    -Printf allows for more precise control over the alignment and width of columns, making it easier to create neatly aligned tables of data. Print and println do not provide this level of control and can result in misaligned columns if not carefully managed with manual spacing.

  • How can you force printf to print a new line at the end of the output?

    -You can force printf to print a new line at the end of the output by including the newline character ' ' at the end of the format string. Alternatively, you can use the '%n' escape sequence, which is platform-independent and will work across different systems.

Outlines

00:00

💻 Introduction to printf Formatting

This paragraph introduces the concept of using printf in Java for better formatting of output compared to print and println. It explains the use of special characters within printf to format strings, integers, and characters. The paragraph demonstrates how to use %s for strings, %d for integers, and %c for characters. It also discusses the common use of printf for displaying currency with two decimal places using %f, which is crucial for financial formatting where trailing zeros are significant. An example is given where a double variable representing money is formatted to show two decimal places, ensuring the correct display of currency.

05:01

📊 Formatting Columns with printf

The second paragraph delves into using printf for creating formatted columns. It explains how to use %s to specify the width of a string in a column and how padding works with spaces. The paragraph provides an example of aligning strings and integers in columns using printf, where the width of each column is defined by the programmer. It also touches on the use of newline characters in printf statements to separate rows of data, contrasting printf with println which automatically adds a newline. The paragraph concludes with a demonstration of how to adjust column alignment to the left by using a negative sign in the formatting string.

10:04

🔢 Advanced printf Formatting Techniques

Paragraph three builds on the previous discussion by showing how to mix integers and strings in columns using printf. It illustrates how to format an integer with a specified number of spaces and a string with a different specified width, aligning them neatly in columns. The paragraph also explains how to force a new line after each printf statement to separate data entries visually. Additionally, it covers the concept of right and left alignment within columns, showing how to adjust the formatting to push values to the right or left within a specified column width. The paragraph emphasizes the importance of consistent formatting for data presentation.

15:05

📝 Summary of printf Usage in Java

The final paragraph summarizes the key points covered in the video script regarding the use of printf in Java. It highlights the importance of %f for formatting monetary values with two decimal places, %s for aligning text in columns, and %d for printing integers. The paragraph reiterates that printf does not automatically add a newline at the end of a statement, so the programmer must include %n if a newline is desired. It wraps up by emphasizing the practical applications of printf for beginners in programming, particularly for tasks like formatting currency and creating neatly aligned columns of text.

Mindmap

Keywords

💡printf

printf is a method in programming used for formatted output. It is similar to print and println in Java but offers more control over how the data is displayed. In the video, printf is used to demonstrate how to format output, such as displaying money with two decimal places using '%.2f'. It is a core concept as it is the main tool discussed for formatting strings and numbers in the script.

💡formatting characters

Formatting characters are special symbols used within printf to define how different data types should be displayed. The script introduces '%s' for strings, '%d' for integers, and '%c' for characters. These characters are crucial for understanding how to control the output format in printf, as they dictate where and how variables are inserted into the output string.

💡%s

The '%s' formatting character is used in printf to represent a string. In the video, it is used to format output for strings, such as 'column1' and 'column2', ensuring that they are displayed in a specific number of characters, padding with spaces if necessary. This is important for creating aligned columns in the output, as seen when the script discusses printing values in a tabular format.

💡%d

The '%d' formatting character is used to represent an integer in printf. It is mentioned in the script as a basic formatting character for displaying integers. The video uses '%d' to demonstrate how integers can be formatted in printf, which is essential for understanding how to print numbers in a structured way.

💡%c

The '%c' formatting character stands for a character in printf. It is one of the basic formatting characters introduced in the video. Although not extensively used in the provided script, it is part of the foundational knowledge required for using printf to format output correctly.

💡%.2f

The '%.2f' formatting character is used to display floating-point numbers with two decimal places. This is particularly important when dealing with monetary values, as shown in the video. The script uses '%.2f' to format the 'money' variable to ensure it always displays two digits after the decimal point, which is a common requirement for currency formatting.

💡variable

Variables are used in printf to insert values into the formatted string. The video script mentions variables like 'money', 'x', 'y', 's1', and 's2', which are then formatted using printf. Understanding how to use variables with printf is key to dynamically generating output based on program data.

💡columns

Columns refer to the arrangement of data in a tabular format, where each column represents a category of data. The video uses printf to align data in columns, demonstrating how '%s' can be used to specify the width of each column and ensure data is displayed neatly. This is a practical application of printf for creating user-friendly output.

💡right alignment

Right alignment in printf is achieved by using a negative sign before the width specification in the formatting character. The video explains how to left-adjust data in columns by using a negative width, which is a technique for aligning numbers and text in a way that makes the output easier to read and compare.

💡newline

A newline is a special character that moves the output to the next line. In the context of printf, newlines are not automatically added, so the script mentions using '%n' or 'println' to create a new line after the formatted output. This is important for separating output into readable sections.

Highlights

Introduction to printf as a method for formatted output in Java.

Explanation of special characters used in printf for formatting output.

Use of %s for string formatting in printf.

Utilization of %d for formatting integer values in printf.

Introduction of %c for character formatting in printf.

Demonstration of printf for displaying monetary values with two decimal places using %f.

Clarification on how printf handles floating-point numbers and the significance of %f.

Explanation of %f with a specific example of money formatting.

Use of printf to format and display variables within a string.

Tutorial on aligning columns using printf with %s and specified width.

Example of using printf to create formatted columns with strings and integers.

Discussion on how to force a new line after printf output.

Technique to left-align text in printf columns using negative width values.

Practical application of printf for creating neatly aligned columns in output.

Summary of the most commonly used printf formatting characters: %f for money, %s for columns, and %d for integers.

Emphasis on the importance of manually adding a new line with %n in printf statements.

Transcripts

play00:00

okay in this video we're going to work

play00:04

with print F and print F work with

play00:09

printf printf is another method just

play00:14

like print line and print in system dot

play00:16

out but it allows you to format your

play00:21

output better than print and print line

play00:25

thus the print F so there are some

play00:30

special characters you need to know

play00:34

inside a printf for it to be used so I'm

play00:40

gonna create maybe a header right here

play00:46

I'm gonna kind of follow this one down

play00:49

right and I'm gonna type some of these

play00:54

characters that we're gonna use at first

play00:56

and we're gonna build upon them as we go

play00:58

so I'm gonna use % s right first which

play01:06

is for a string I'm gonna use % d for an

play01:12

integer decimal right integer and what

play01:19

else are we gonna use at the beginning

play01:21

maybe a % C for our character right

play01:25

these are the basics right for our

play01:28

character char right okay so let's talk

play01:33

about how print F works if I have a

play01:37

variable like int or let's use a double

play01:42

or a double money and I assign it a

play01:48

value like 1.7 one of the most commonly

play01:52

used things inside a printf is when you

play02:00

need to display money and here's why

play02:03

let's say I was going to print that

play02:04

amount of money out using a print line

play02:07

so I say system dot out dot print line

play02:10

use

play02:11

and then I'm gonna put a dollar sign

play02:14

right and then I'm gonna append money to

play02:18

it and then I'm gonna say you know at my

play02:24

store right so what's this gonna do this

play02:26

is gonna print out you spend you know a

play02:29

dollar seventy with a dollar sign at my

play02:33

store so if I run this code right I

play02:35

would get this now we can all tell that

play02:39

this doesn't look right why doesn't it

play02:41

look right it doesn't look right because

play02:43

we don't display money as 1.7 we display

play02:47

it as 1.7 Oh

play02:49

however for floating point numbers or

play02:51

for doubles even if I put the 1.70 there

play02:57

right and and run it it's still gonna do

play03:01

a dot 1.7 because the troweling zeros

play03:04

aren't needed and java knows this right

play03:06

in doubles so sometimes it's useful to

play03:11

print out money that has two places

play03:18

after the decimal point right so let's

play03:26

see how we can do this so up here I'm

play03:29

gonna add another one because I started

play03:31

with money right I'm gonna come up and

play03:33

say percent half for floating point

play03:36

right float or double right okay so what

play03:42

we can do is we can use a printf and we

play03:45

use it almost the same way we say system

play03:47

dot out dot print F right now eclipse is

play03:52

gonna help you out it's going to put

play03:54

self in there I'm gonna delete that for

play03:55

now now the way pronounced work is we

play03:57

put a quote with something in it right

play04:00

and then we follow it with as many

play04:03

variables as we want we don't mix quote

play04:06

with variable with more quotes like we

play04:09

do in print line we put everything we

play04:11

want up here in the quotes and then we

play04:14

put comma variable come a variable come

play04:16

a variable come a variable depending on

play04:18

how many variables there all right now

play04:19

in this case there's only one so what

play04:21

I'm gonna do inside the quotes is I'm

play04:23

gonna write you spend and I'm gonna put

play04:27

a dollar sign all right and then I'm

play04:30

gonna leave a space for now at my store

play04:34

all right and then I'm gonna put comma

play04:38

and the variable I want printed right

play04:41

money okay now I need to tell the printf

play04:46

where in this string where in this

play04:49

string I want the value of money to be

play04:52

displayed and I want it to be displayed

play04:53

right here right after that power sign

play04:56

right so what I'm gonna use is my

play04:59

special formatting characters which are

play05:01

up here percent as percent d % c % F

play05:04

right now % F is a floating point number

play05:09

now the floating point number takes some

play05:14

characteristics I could say % point 2f

play05:19

now let's explain what % point 2f meets

play05:23

% point 2f means I want to use two

play05:29

decimal places after the decimal place I

play05:33

want two values printed after the

play05:35

decimal place regardless of how many

play05:38

values there are so if there were a

play05:40

hundred values after the decimal place

play05:41

let's say I had one point seven nine six

play05:43

four two it's only gonna print the first

play05:46

two right I could also put a number

play05:49

before the decimal point like five and

play05:51

that's gonna use five spaces before the

play05:55

decimal point no matter how big the

play05:57

money is but for money I don't care how

play06:00

many are used before it I can just let

play06:02

that be whatever it is so % point 2f is

play06:06

one of the most commonly used formatting

play06:08

to print money so it's gonna take this

play06:10

value of money and it's gonna fill it in

play06:13

right here right where that percent F is

play06:16

% point 2f and it's gonna use the

play06:19

formatting which says put two spaces

play06:21

after the decimal point

play06:23

so if I run this now now I can see that

play06:26

my second line the one that uses the

play06:28

pronoun

play06:29

used 1.70 now let's just make some

play06:32

changes here if I used 0.4 F then what's

play06:36

gonna happen it's gonna fill with 4 0 4

play06:41

values after the decimal point right so

play06:43

clearly 0.2 F is the way that we want to

play06:48

display money okay

play06:50

let's say we were going to display

play06:55

columns again remember we were doing

play06:58

columns right so we could use strings

play07:05

for columns so what could I do instead

play07:08

of trying to line things up I could use

play07:10

print apps so let's we'll leave our

play07:12

money example right cuz that's not a bad

play07:15

example and then we're gonna declare

play07:20

let's declare some values right I could

play07:26

declare a couple integers into x equals

play07:33

equals 5 int y equals 100

play07:42

then let's declare some string string 1

play07:45

s1 equals you know hello and string s2

play07:52

equals goodbye right okay okay so now I

play08:00

have some strings so now let's use those

play08:02

so let's assume I wanted those to be

play08:06

displayed in some columns so I could use

play08:10

print line right and I can put column 1

play08:14

and then column 2 but we went down this

play08:18

road before that I have to figure out

play08:21

how to put spaces in between all the

play08:24

others right and it gets very confusing

play08:27

right so what I could do is I could do

play08:30

this printf okay so let's see how we

play08:32

would use a print F instead of print

play08:36

line I'm going to use printout now print

play08:37

off again takes a quote with all our

play08:41

formatting in here

play08:43

then it's followed by the variables we

play08:45

want printed the variables or strings or

play08:49

any or values right so what I can do is

play08:51

I can use % s appear and I could say I

play08:55

want I want 15 characters ask something

play09:02

using his first string so I want 15

play09:04

characters in a string right and then I

play09:07

want the second column to be I don't

play09:10

know we can make it whatever we want we

play09:11

can make it 15 also I could make it 12

play09:13

for any 12s and then I want it to print

play09:22

so I'm gonna put comma and I don't have

play09:26

variables for column 1 and column 2 so

play09:28

I'm just gonna put those strings column

play09:30

1 and the next thing I want printed is

play09:34

column - so let's dissect what I did in

play09:37

this line so printf takes this first

play09:41

formatting this first string is

play09:44

formatting right so then it takes after

play09:47

the Komet takes whatever's first here

play09:50

column 1 and it puts it into the first

play09:52

formatting here so it's going to use 15

play09:54

columns to print column 1 and then it

play09:58

takes the second thing passed in and it

play10:00

puts it into the second formatting so

play10:03

it's going to use 12 columns Supreme

play10:05

column 2 so it's padding it with spaces

play10:08

just like the point 2f padded with zeros

play10:11

because it was a half a floating-point

play10:14

number percent s is gonna pad with

play10:17

spaces so if I just ran this code the

play10:21

way it is I would get the line that says

play10:24

column 1 and column 2 and they would the

play10:27

first one would be using 15 characters

play10:29

and the second would be let's put a new

play10:31

line in between here so printf is like

play10:36

print it's not like print line it does

play10:39

not put a carriage return at yet so you

play10:41

have a couple ways to do that I could

play10:43

put in between these I could do system

play10:46

dot out dot print line right and type

play10:48

that or I could use special character up

play10:51

here just like in print and print line

play10:53

we have backslash on

play10:55

in print F we have percent and actually

play10:58

backslash and it also works but it's

play11:00

better to keep it consistent with

play11:01

percents so now this first line with the

play11:04

money is going to have a carriage return

play11:06

at the end so it won't all be on one

play11:08

line so let's run this okay so now let's

play11:12

look at this column 1 column 2 it used

play11:15

15 spaces to print column 1 and if you

play11:19

notice right let's count right to be

play11:21

sure right I'm gonna count 1 2 3 4 5 6 7

play11:27

8 9 10 11 12 13 14 15 right so it used

play11:33

15 spaces to fill column 1 and padded

play11:37

with spaces at the beginning and then it

play11:39

used 12 spaces to do column to it right

play11:42

so if I count these I've got 1 2 3 4 5 6

play11:45

7 8 9 10 11 12

play11:46

right so use 12 spaces to use those and

play11:49

then I would like to force a new line at

play11:54

the end so now let's see how we could

play11:56

line up the next lines right so I could

play12:00

use another printf and eclipse is

play12:04

filling it sometimes when eclipse fills

play12:06

things in it's really useful and

play12:08

sometimes when eclipse fills things in

play12:09

it gets in the way but I'll let you

play12:11

decide that so now let's say I wanted to

play12:13

print one of the numbers and one of the

play12:19

strings in each comes socom one will be

play12:22

my numbers and column 2 will be my

play12:24

strings so the first thing I want to

play12:28

print again using 15 spaces is an

play12:32

integer which we use D for that four

play12:35

decimal would be that I agree it would

play12:37

be better with an eye but it is not okay

play12:39

and then the second column is going to

play12:41

be a string so I'm going to use percent

play12:43

s but I wanted to use 12 spaces for that

play12:46

right and then I'm gonna print a new

play12:49

line

play12:50

now the first value I want to be filled

play12:53

in to this decimal is my axe and the

play12:56

second value that I want to be filled in

play12:58

to my string is my s1 so I'm going to

play13:01

put comma X comma s 1

play13:04

I'm gonna run this and if I see what

play13:09

happens when I run it my columns line up

play13:12

really nicely now because I'm forcing

play13:15

the first come to be 15 the second

play13:18

column to be 12 and again it's padding

play13:20

these at the left-hand side it's padding

play13:24

at the left-hand side so now let's say I

play13:26

want my second set of values I could

play13:29

copy this line right and I could pass in

play13:32

Y and s2

play13:35

right and now everything's gonna line up

play13:38

nicely because my print F forces each

play13:43

column to be 15 characters for com1 and

play13:47

forces all column twos to be 12

play13:51

characters right so it's easy to line up

play13:53

now sometimes we may want to that when

play13:59

when we print this out this is called

play14:01

right alignment right or a right

play14:03

adjustment it's forcing all the values

play14:06

to the right hand side of the column

play14:08

right because column one starts over

play14:11

here on the left and ends after the one

play14:13

so the five is right formatted right

play14:16

it's with formatting it to the right and

play14:18

the hundred is pushed to the right I can

play14:20

push to the left also right I can push

play14:23

the left also and the way I do that is I

play14:26

put negative sign in front of the

play14:31

formatting so let's just warm at the

play14:34

left the first column to be left

play14:37

adjusted right so I'm gonna use

play14:39

negatives on the first columns

play14:41

formatting and if we run this now right

play14:45

now I see column one is left formatted

play14:47

right and if I wanted the other columns

play14:49

to be left adjusted right I could use a

play14:52

negative sign there also right and if I

play14:57

run it now it's all left adjusted so

play15:00

it's padding the spaces to the right

play15:02

right it's padding it out to 15 here

play15:04

it's padding it out to 15 right here

play15:06

it's okay so so the ones you're most

play15:12

likely going to be used using the most

play15:14

the first one you're going to use the

play15:16

most

play15:16

is the percent F and it's for money and

play15:19

% point 2f is the most commonly used

play15:23

reason for print F in your beginning

play15:26

programming because you don't want money

play15:28

to be to be displayed as one point seven

play15:31

you want it to be displayed as one point

play15:33

seven zero so % point 2f takes care of

play15:36

that percent s is very useful for

play15:40

columns as you saw right we we line up

play15:43

columns right percent D is incredibly

play15:47

important because typically we're

play15:48

printing out integers right and then

play15:51

percent F or sorry percent C for

play15:54

characters we can pad those spaces out

play15:58

again this is a general introduction to

play16:01

printf a couple things to remember

play16:04

printf is like a print statement it does

play16:07

not put a new line at the end you have

play16:10

to put the % n at the end of the string

play16:13

if you want a new line to appear after

play16:16

the print F ends

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Java ProgrammingPrint FormattingCode TutorialString ManipulationInteger HandlingFloating PointMoney FormattingColumn AlignmentProgramming TipsCode Readability
¿Necesitas un resumen en inglés?