Skip Top Navigation Bar

Console Input and Output

Overview

In this lesson, students will learn how to accept input from a user and produce console output.

Learning Objectives

Skills

Student Outcomes

Students will be able to:

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.

Using the IO Class for Input

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:

  1. they can consist of letters, numbers, underscore (_), and dollar sign ($). By convention, the dollar sign ($) is never used.
  2. they cannot start with a number. By convention, they always start with an uppercase letter.
  3. 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() {

}

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.

Input

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.

Image the program code in Visual Studio Code with an input of Crystal and provided output of Hi Crystal! Are you having fun with Java?

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:

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.