Java Scanner: What Is It & How Do You Use It?

Athena Ozanich
Athena Ozanich

Published:

In software development, the program will inevitably require information for the user at some point. For example, some websites may only require an age to view the content, while video games require a constant flow of user input. However, capturing user input and putting it to use is a subject that tends to be under-discussed, so let’s talk about it.

Woman taking notes on the Java Scanner class and how she can implement it in her software.

In this post, you will discover the Java class called Scanner and how to create an instance of it. You will also learn about the different methods that come with it and how they extend or specify the behavior of the scanner class. Finally, you will see some code examples from the view of developing a simple character creation phase for a hypothetical video game.

Without further delay, let’s jump right in.

Download Now: 25 Free HTML & CSS Hacks

What is the Java Scanner?

The Java Scanner is a class in the Java Utilities Package, and it serves the purpose of creating bi-directional communication between the software and its user. In other words, it provides a way for the user to enter information that the software can use to inform its behavior.

This is a big deal since almost all software requires some degree of user interaction, and it needs a way to capture that data. The creators of the Java language considered that early on and provided the Scanner class to make this an easy process. Quite right, too, since it should be one of the easiest things to do, especially for software that requires a large amount of user input.

Those building websites with Java can use it to create a user sign-up/sign-in feature, ensuring that only the characters the developers deem safe can be entered. You can even use the Scanner to control data format before entering it into a database. The following video shows an example of how to use the Java Scanner class to perform a function.

How to Import Scanner in Java

Importing Scanner for use in your code is simple and only requires a singular line of code. The Scanner class comes in a commonly used package aptly named the utilities package. At the top of every file is where all imports occur, and the utils import will often be one of the first imports you make.

 
import java.util.Scanner;

And that’s it; importing the Scanner class is as easy as it can get. Once you’ve imported the Scanner class, you can begin to use it in your code. Next, let’s look at some of the most common methods used with the Scanner class, then we can dive into some code examples.

Java Scanner Methods

The Java Scanner class also comes with several methods to create more robust input collection techniques. In addition, scanner methods can help develop stringent rules and guidelines for user input, like the type and the amount of data it can accept.

Combining the Scanner with other functions, methods, etc., makes it an even more powerful tool. For example, you can ensure that only permitted characters get validated and entered into the software database. In video game development, you can use it to build a character creation phase, where a user will enter information like their name.

  • nextInt(): Reads a single int value from the input. (Format: Unbroken number)
  • nextFloat(): Reads a single float value from the input. (Format: Up to 32 digit decimal)
  • nextBloolean(): Reads a single boolean value from the input. (true/false)
  • nextLine(): Reads a single line value from the input. (stops at \n)
  • next(): Reads a single word value from the input. (stops at “whitespace”)
  • nextByte(): Reads a single byte value from the input.
  • nextDouble(): Reads a single int value from the input. (Format: Up to 64 digit decimal)
  • nextShort(): Reads a single short value from the input. (-32,768 to 32,767)
  • nextLong(): Reads a single long value from the input. (-9,223,372,036,854,775,807 to 9,223,372,036,854,775,808)

How to Use Scanner in Java

Let's consider the Scanner from the view of a game development standpoint, which is transferable to other software development. Considering the character creation step in a video game, most require input. Typically during this step, the game will prompt the user for information; this is often a name, height, age, etc.

Once the user enters this information, the game uses it to perform specific actions, like inserting the character’s name into the speech in the game or setting the character's height.

Now, let’s look at an example of how this works with the Java Scanner.

 
class Main {
  public static void main(String[] args) {
    Scanner userInput = new Scanner(System.in);
    System.out.println("Please tell us your name:");
    // String input
    String name = userInput.nextLine();
    //Pint user input
    System.out.println("Welcome to Gallifrey: " + name);
  }
}

The code above would prompt the user to input their name, and the user would type and enter their text; let’s assume the user entered Doctor Who. Once that action finishes, the code above would print “Welcome to Gallifrey, Doctor Who” to the interface. With this example in mind, it becomes easier to understand how the methods above fit into place with this class.

For example, instead of calling the nextLine() method on our input Scanner object, you could use the next() method.

 
// String input
String name = myObj.next();

This line of code would have only returned the string Doctor dropping the rest of the string. This behavior can be beneficial for controlling the data flow to and from the software. Check out the next video to learn more about how this process can be used.

How to Start Scanning for User Input With Java

You’ve learned a lot throughout this post, such as the Java Scanner class and what it does. You’ve also learned how to import it and understand the methods that come with it. You’ve even seen a simple yet effective example of how you could use it to collect a character name from a user.

New Call-to-action

Topics: Java

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about HTML and CSS and how to use them to improve your website.