Common Print Methods
When you looked at my sample code from our first unit, you noticed that information was printed to the console. There are many ways to present the end-user with information, but in Program Logic, we will use some built-in Java print methods. This chapter has several code snippets that show you how to use the various print statements. Please carefully review all code and outputs on this page.
Requesting Information from the End-user
Please remember, the end-user is not reading your code, therefore, it is essential that you tell the end-user what you want from them. Be specific and give an example if needed. For instance, if you want the end-user to enter a phone number in a specific format, then you should include an example of the format in your statement to them. Giving information to the end-user is typically in the form of a string literal. Below is an example of requesting information from the end-user. Keep in mind, when you request information, you need code to gather the input, which is covered in the Scanner chapter. Please study the code below.
System.out.println("Please enter your phone number #-###-###-####");
System.out.println()
Instead of saying “println”, I will say “print line”, which better represents what you are doing. The Java statement System.out.println() is used to print data to the console. After printing, the cursor in your console will be on the line underneath whatever was printed. If you do not have anything between the parentheses, you will get an blank line. This technique is often used when presenting a multi-line output. Whatever you put between the parentheses, is printed to the console. You can combine string literals with variables to print a message to the end-user. Notice the spaces, periods, and comma in my sample code below. System.out.println() does not format your output. The plus signs are concatenating string literals to the variable, fullName. Please study the code below.
System.out.println("Hi, " + firstName + ". I am pleased to meet you.");
If the value of firstName is Bruce, then the output of the above code would be:
Hi, Bruce. I am please to meet you.
System.out.print()
Another Java print method is .print(). The .print() method requires something between the parentheses. If you do not have something between the parentheses, you will get a no suitable method found error. When you use the .print() method, the cursor in your console will be on the same line as whatever was printed. I typically use System.out.print() in conjunction with the .printf() method, which allows some formatting. As with the .println() method, you can combine string literals and variables to produce a message to the end-user. Remember, the .print() method does not format your output.Please study the code below.
System.out.print("The total is: " + total);
If the value of total is 15.99, then the output of the above code would be:
The total is: 15.99
Remember, string literals are always in quotes whereas variables are not.
System.out.printf()
The Java syntax used in the .printf() method is similar to syntax you will find in other languages. The syntax takes a bit to get your head around and can be used in more scenarios than I show in this chapter. I mainly use the .printf() method to format numbers. Using the .printf() method to format numbers is okay in Program Logic, however, if you continue to my Java courses, you will learn more sophisticated techniques. Please study the code below.
System.out.printf("%.2f", price);
In the above statement, “%.2f” is the format string, meaning that is how we want the data formatted. In this case, the data is the value within the variable price. The “f” says the value must be a double or float data type, so “%.2f” means we want to format the value of price to two decimal places. You always need to put the format string in quotes. Please note, rounding rules apply.
If the value of price is 154.9852, then the output of the above code would be:
154.99
Remember, I said I usually use print() with printf(). Below is an example of using the two. Please study the code below.
System.out.print("Your price is $");
System.out.printf("%.2f", price);
If the value of price is 154.9852, then the output of the above code would be:
Your price is $154.99
Notice that my output is more than just the value. I add some qualifying words to create a nice sentence. Please do the same in your assignments.
What You Learned
- The
.println()method places the cursor below what was just printed - If you do not have anything between the parentheses, then
.println()method gives you a blank line - The
.print()method places the cursor on the same line as what was just printed - You must have something between the parentheses when using the
.print()method - Neither
.print()or.println()will format your data for you - You can use the
.printf()method to format adoubleto two decimal places - String literals are always in quotes
- Variables are never in quotes
- You can and should combine words with variables to give a meaningful message to the end-user
- When you use a variable, its value is printed to the console
What's next?
The next chapter in this unit is: End-user Input and the Scanner Object