The Scanner Class

Code Bar
Scanner A class which reads and returns information from an input to the code.
.next() Returns the next phrase in the input which is separated by spaces. Returned value is a String.
.nextLine() Returns the next phrase in the input which is separated by a new line. Returned value is a String.
.nextInt() Returns the next phrase in the input which is separated by spaces. Returned value is an int.
.hasNext() Returns a boolean which reads true if the input has another value to be read.

Scanner is a useful class which allows the code to get information from the user or a file. This allows the code to use data which did not originate in the program, so it can be used for much more.

Each of these methods will return a value, each one returns a different type. If the input cannot be converted into the correct type, the method will return an error. Here is the syntax for Scanner using keyboard input to the console:

    Scanner s = new Scanner(; 

The scanner class is now a variable defined as s however it can be whatever you decide to name it. refers to keyboard input, but Scanner can read input from other sources like files as well. To change the input source, change the information in the parentheses. Here is the syntax for Scanner using file input:

    Scanner a = new Scanner (new File ("FILENAME"));

To use the methods from Scanner, you have to use the following syntax:; //this calls the method .next() from the keyboard input; //this calls the method .next() from "FILENAME"

Here is an example of using keyboard input with Scanner:

    Scanner s = new Scanner(;

    String name = "";

    System.out.println("Please enter name: “);

    name = s.nextLine();

    System.out.println("Your name is “ + name);

This program will store the next line of text from the keyboard as the String name.

The last requirement for using the scanner class is to import Scanner, and if you reading from a file, also import File and FileNotFoundException. Importing is a process which stores the information of irregular classes in a class which will use them. Here is the syntax for importing these classes:

    import java.util.Scanner;   



    // these need to be above the class signature line

Then, if you are reading from a file, replace the main method header syntax with the following code to allow your program to compile:

    public static void main(String[] args) throws FileNotFoundException {

Most classes are not used on a regular basis; therefore, to improve data storage, only especially common classes are in the standard class library (a list of classes which is automatically stored in every program). Understanding how to import classes is essential to ensuring that your code will compile.

Lesson Quiz

1. What is the syntax of declaring and initializing a Scanner class variable, l, which derives input from a file, h.txt?

a. Scanner l = new Scanner(h.txt);
b. Scanner l = (new File("h.txt"));
c. ( = new Scanner l;
d. Scanner l = new Scanner(new File("h.txt"));

Written by James Richardson

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.