Getting Started with Java Programming

Output and Input

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video segment presents how Java handles output with System.out and how it handles input with the Scanner class.

Keywords

  • input
  • output
  • concatenation
  • Scanner
  • exception
  • System

About this video

Author(s)
Kenneth Fogel
First online
02 July 2022
DOI
https://doi.org/10.1007/978-1-4842-8614-2_7
Online ISBN
978-1-4842-8614-2
Publisher
Apress
Copyright information
© Ken Fogel 2022

Video Transcript

Let us take a closer look at output using System.out.println. The most common way to display information that we have already seen in earlier examples. Then we will look at how we get user input from the keyboard.

Output is handled by one of two Java library methods, printIn and print. They’re called from the System.out object. Displaying to the console is so common that this object has been instantiated by the Java Virtual Machine automatically. They are ready to use anywhere or any time in your program.

The printIn method advances to the beginning of the next line after the string it displays. Notice, the cursor is under the letter M. This is the beginning of the next line. The print method does not advance the cursor. And it remains at the end of the string it displays. Here we see the cursor right after the letter E in moose.

Here is a simple program that simply displays a single line. The moose is a majestic animal. Let’s see what it looks like when we run it. When we run this program, notice that there is a blank line immediately after the string. Let’s modify the program to use print instead of printIn. Let’s remove the ln and just leave behind print. We’ll save our file.

Let’s run this again with just print. Notice, there is no blank line after the string. This tells us that printIn added a new line and print does not. We can combine different types using the concatenation operator when we display a string. We can combine a string with any primitive type. In this case, we’ve decided the moose population is 1 million, and now I can display that there are as many as, well, 1 million mooses in Canada.

To get input from the user, we must use the Scanner class. It requires us to have an import statement so that the Java compiler knows we are using this particular library, and then we must instantiate it using new and providing to it the name of the object that manages the keyboard, which in Java is called System.in.

Once you instantiate the scanner, you can call upon one of the next methods. There is a method for every primitive data type plus string. Here we can see that we will assign the value returned by the next method to the appropriate variable. You should precede each input with a print statement that informs the user what is expected of him. This is called the prompt.

Next methods halt a program until you enter the required information and press the Enter/Return key. Except for the string, input is converted to the primitive type requested, entering a string that cannot be converted into a primitive results in an input mismatch exception.

Here is a simple program that allows us to see the Scanner in action. We begin by instantiating the Scanner, we follow it with a prompt, and then we use the appropriate next command to gather your input. Once entered, we then display the result. We’re going to run this program twice. Once, doing what we’re told, entering an integer, and then a second time watching what happens when we do not enter something that can become an integer.

Here we are ready to run our program. It asks us to enter an integer, which I shall do as I’m told, and then when I press Enter, it displays the information I’ve entered. Let’s run this a second time, but this time let’s use my favorite number “bob”. Now I will enter bob, and when I press Enter, we get an exception, specifically an input mismatch exception. The characters bob could not be converted into an integer.

In the section coming up on loops, we will learn how to handle input mismatch exceptions. Next up, let’s take a look at how we design programs using what we’ve learned up to this point.