Console Input and Output
Overview
In this lesson, students will learn how to accept input from a user and produce console output.
Learning Objectives
- 1.1.C.4 Write and interpret output statements.
- 1.1.C.5 Write and interpret input statements for values being provided from an external source.
Skills
- S1.B Design algorithms for a program.
- S1.D Explain the impact design has on data storage.
- S2.A Write program code and implement algorithms.
- S2.C Analyze an algorithm and program code for correctness.
Student Outcomes
Students will be able to:
- prompt users for input
- accept input into the program from a console
- write output statements to the console
Duration: 1 class period
Resources
Background
So far we have been working with values that have been included in the program code. Now we would like to allow users to interface with the program and allow them to enter values.
Typically when input is required, there is a prompt to the user. We will be using the IO
class to accept input into the program.
Introduction to Methods
To gain input we use methods. A method is a block of code that has been defined to perform a specific task or algorithm. Methods can have parameters that allow values to be made available to use in the algorithm. Methods can have a return value to provide a result of the algorithm.
Methods can be static methods. Static methods are methods that belong to the class and should be called using the class name instead of an object.
Methods can be instance methods. Instance methods are methods that belong to the object and are called using an object of the class.
The IO
class contains static methods for input and output.
The IO
class is in preview for Java 24. It is part of the java.io.IO package. The class has been implicitly imported for your use and calling the methods does not require the class name. This will change in Java 25, so watch for updates.
The IO
class contains the following methods to use for both input and output.
IO Class Methods
Method |
Method Description |
print(Object obj) |
Writes a string representation of obj to the console. Does not move to the next line after the string representation of obj is printed to the console. Any new calls to print will continue directly after what was printed. |
println() |
Stands for print line. Stops printing on the current line and moves to the next line. |
println(Object obj) |
Writes a string representation of obj to the console and moves to the next line. Any new calls to print or println will start on the next line. |
readln() |
Reads a single line of text from the console.
|
readln(String prompt) |
Writes prompt to the screen as if calling print . Then, reads a single line of text from the console. |
Output
The Java Playground is great for trying out segments of code, but if we write print
statements to show that the output doesn't move to the next line, it won't behave as we expect it to. That is because it will treat each print
statement distinctly. At this time, to test out some code, we will want to move over to an IDE. If you need help, please reference the Getting Set-up page.
In this section, we will be using preview features in JDK 24. To use features that are still in preview, you will need to enable preview features in your IDE.
The main Method
When using an IDE, we need to write a class in Java. This class will have one method, the main
method. With preview features turned on, creating the class is really easy, you just need to create a new Java file in your IDE and name it with the extension .java
. The name of the file will be the name of the class and should start with an uppercase letter. The naming of a class follows some of the same rules as naming variables:
- they can consist of letters, numbers, underscore (
_
), and dollar sign ($
). By convention, the dollar sign ($
) is never used.
- they cannot start with a number. By convention, they always start with an uppercase letter.
- they cannot be a Java keyword. A Java keyword is a word that is set aside with a specific meaning in the language. Our primitive data types, for example, are all considered Java keywords.
This first class will only have one method, the main
method. The header for this method will be as follows:
void main() {
}
void
is the return type for this method. A return type provides the data type of a value that would result from calling this method. This is referred to as returning a value. Not all methods have a value that is being returned. When there is no value being returned the void return type is used.
main
is the name of the method.
()
represents the parameter list. In this case, there are no parameters in the parameter list. Parameters are values that are being made available for the method to use when executing its algorithm.`
{}
are used to denote the start and end of the code segment that is in the body of this method. This is the program code that would be executed when the program is run.
After you enable preview features, create a new java file called SampleOutput.java
that contains the following code:
void main() {
print("Let's play with Java!");
}
When you run this code, you should see the following in the console window: Let's play with Java!
For each of the following examples, modify the body of the main
method to be the code segment provided.
Code Segment 1:
String name = "Duke";
print("Hi " + name + "! Are you having fun with Java?");
This should print:
Hi Duke! Are you having fun with Java?
The value of name is added to "Hi " giving us "Hi Duke". Then "! Are you having fun with Java?" is appended giving the final resulting value that is printed to the console.
Code Segment 2:
print("This is on the first line.");
print("This is also on the first line.");
This should print:
This is on the first line.This is also on the first line.
Code Segment 3:
print("This is on the first line.");
println();
print("This is on the second line.");
This should print:
This is on the first line.
This is on the second line.
Code Segment 4:
println("This is on the first line.");
print("This is on the second line.");
This should print:
This is on the first line.
This is on the second line.
If you would like to get values from the user, you can prompt the user for the input.
Modify the main
method used for the output samples as follows:
String name = readln("Enter your name");
print("Hi " + name + "! Are you having fun with Java?");
This will allow you to enter your name into the console as input. For this example, if I entered in Duke
, the following would be output.
Hi Duke! Are you having fun with Java?
Here is an example of what it could look like in Visual Studio Code.
Activity
Activity 1: Mini-Lab
Have students write a short program where they take in the number of hours worked and the program tells them their pay if they get paid $8 an hour. Ask students to do some planning by answering these questions:
- What data is the program using?
- number of hours worked
- amount they get paid per hour
- What data is being produced?
- the amount they would get paid
- What is the type of this data?
- hours worked is an
int
or a double
if they want to allow partial hours worked
- amount they get paid is a constant and in this case can be stored as an
int
- amount paid can be an
int
if the other data values are both int
, otherwise the type double
should be used
- What are good variables names for this data? Some examples:
- hoursWorked
- hourlyRate
- amountPaid
- What are the steps to write the program?
- declare variables and constants
- prompt the user to input the hours worked
- compute the amount they would get paid
- output this value to the user
Once they have a plan, they should put the code into the IDE and run it.
Extension
Have students modify the program to obtain the pay rate from the user as well.