1️⃣

5.1 Introduction to Scanner

Chapter Table of Contents

🔍
Ch. 5 Scanner

Section Table of Contents

What is Scanner Conceptually?

So far we have used predeclared variables for values. But what if values were given or input to us by a user?
Scanner gets user input, which you see day to day in: login systems, games, chat rooms, and more!
How Scanner works: the Scanner class is imported, a Scanner object is made from it, and then the object is used to read in different input such as numbers or strings.

An example of how Scanner is used

Download the file FullName.java and run it. The output could look something like this, but will most likely look different depending on what you give the program as input.
💡
The yellowish text is what the user types in.
What is your first name? Inej Whaht is your last name? Ghafa Your full name is Inej Ghafa
 
Pretty cool, right? You can now interact with the computer! But what’s happening under the hood? Let’s take a look at the actual code that makes up this program.
import java.util.Scanner; public class FullName { public static void main(String[] args) { // create a Scanner object called input Scanner input = new Scanner(System.in); // prompt the user for their first name System.out.print("What is your first name? "); // read the input as a String String firstName = input.nextLine(); // prompt the user for their last name System.out.print("What is your last name? "); // read the input as a String String firstName = input.nextLine(); // print the user's full name System.out.println("Your full name is " + firstName + " " + lastName); } }

Importing Scanner

So let's break it down. First of all the import. You can import (bring) pieces of code from libraries so that you don't have to code it all from scratch. The java.util package (something containing a bunch of similar classes) has useful classes that you can utilize, such as Scanner.
 
💡
To import a class, simply use the import keyword and then the location of the class:
import outerpackage.innerpackage.ClassName;
 
In the case of Scanner, it is located in the util package, which in turn is located in the java package, which comes standard in the JDK that you downloaded in Ch. 1.
⚠️
Note that there can be several outer/inner packages. For example:
import outerpackage.innerpackage1.innerpackage2.ClassName;
 
You also can import all of the classes in a given package. You can do so using the wildcard import, which is denoted by a *.  For example, the following code imports all of the classes inside the java.util package:
import java.util*;
 

Constructing a Scanner Object

Now we can put Scanner to work. First, we need to construct a Scanner object. This is a fancy way of saying that you create a Scanner, which you use to read input. If you don’t quite understand everything in the code, that’s ok. You just have to know how it’s done:
Scanner input = new Scanner(System.in);
 
We are creating a new variable called input which is of type Scanner. Then, we assign the value of input to new Scanner(System.in), which in short creates a new Scanner. The System.in basically means we’re reading input from the keyboard.
 
The Scanner name can be however you want, just like a variable as long as it follows Java naming rules. Usually, people call Scanners input or reader.
 

Reading Input Using Scanner

Once the Scanner has been created, we can call methods on the Scanner object. That is a fancy way of saying that we tell the Scanner to do things. We can call methods in the following format:
 
nameOfObject.nameOfMethod();
In the example above, we used the nextLine() method, which reads the next String that is typed into the console by the user, and then we assign the value that was read into the variable firstName and lastName:
 
String firstname = input.nextLine(); // ... some more code ... String lastName = input.nextLine();

Next Section

2️⃣
5.2 Different Types of Input
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.