Java W3Schools Scanner Guide Learn Code Input Examples

Robert Jul 13, 2026

Java, a popular programming language, provides numerous libraries and classes that simplify tasks such as reading and parsing data from various sources. One of these powerful tools is the Scanner class, which can be exceptionally useful when dealing with keyboard inputs or reading data from files. W3Schools, a renowned online learning resource, offers a comprehensive guide on using the Java Scanner class, which we'll delve into in this article.

Java Input Using Java Scanner
Java Input Using Java Scanner

Whether you're new to Java or seeking to refresh your understanding of the Scanner class, this guide aims to provide a clear and concise walkthrough. By the end, you'll be equipped to handle user inputs and read data from files efficiently.

Deep dive into the java scanner class
Deep dive into the java scanner class

What is the Java Scanner Class?

The Java Scanner class is used for reading data from either the keyboard or a file. It can be an invaluable tool when you need to accept user input or retrieve data from a file in your Java application. Let's dive into the details of how to use the Scanner class, as explained by W3Schools.

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet 🎯💻

One of the key features of the Scanner class is its ability to read data in various formats, including integers, doubles, strings, and more. This versatility makes it a powerful tool for input/output operations in Java.

Importing the Scanner Class

[2020]-Programming in JAVA - Scanner Class Examples
[2020]-Programming in JAVA - Scanner Class Examples

Before you can use the Scanner class, you need to import it into your Java program. This can be done by adding the following line at the beginning of your Java file:

   (>import java.util.Scanner;)

The Scanner class is located in the java.util package, hence the need for importing it explicitly.

Creating a Scanner Object

Add two numbers in java using scanner|| by thunder
Add two numbers in java using scanner|| by thunder

After importing the Scanner class, the next step is to create a Scanner object. This can be done in two ways:

  1. To read data from the keyboard, use the following code:

   Scanner myObj = new Scanner(System.in);
  • To read data from a file, first, create a File object, then pass it to the Scanner constructor:

  • Java Cheat Sheet: Operators, Scanner & User Input ☕
    Java Cheat Sheet: Operators, Scanner & User Input ☕

       File myObj = new File("filename.txt");
       Scanner myScanner = new Scanner(myObj);
    

    Once you have a Scanner object, you can start reading data using its methods.

    User input with Java
    User input with Java
    an image of a computer screen with many lines on the screen and text below it
    an image of a computer screen with many lines on the screen and text below it
    java for complete beginners - user input
    java for complete beginners - user input
    🌟 ☕Java User Input Example | Learn Java Programming Easily
    🌟 ☕Java User Input Example | Learn Java Programming Easily
    Object in java
    Object in java
    java array operations
    java array operations
    ☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
    ☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
    JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
    JAVA - Student Information System Project In Java Using NetBeans With Source Code | Part 0 - ...
    two different types of numbers that appear to be written in the form of an array
    two different types of numbers that appear to be written in the form of an array
    ☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
    ☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
    an uml class diagram with multiple classes in the same language, including jvm and jvm
    an uml class diagram with multiple classes in the same language, including jvm and jvm
    Bus Reservation System Project in Java with Source Code
    Bus Reservation System Project in Java with Source Code
    Java Math.ceil() Method with Examples
    Java Math.ceil() Method with Examples
    a black background with text that reads math with java perform addition, subtraction, multiplication, division, and modulus with numbers
    a black background with text that reads math with java perform addition, subtraction, multiplication, division, and modulus with numbers
    Java Switch Example
    Java Switch Example
    Substring in JAVA - 5 Ways to Use Java Substrings to Boost Code
    Substring in JAVA - 5 Ways to Use Java Substrings to Boost Code
    Java code for maintaining a student records database using File Handling
    Java code for maintaining a student records database using File Handling
    Logical Operators in Java: Think Smarter, Code Better ☕🚀
    Logical Operators in Java: Think Smarter, Code Better ☕🚀
    Top 5 Features of Java 17
    Top 5 Features of Java 17
    Difference Between Java and C++
    Difference Between Java and C++

    Using the Scanner Class Methods

    The Scanner class provides several methods for reading data in different formats. Let's explore some of the most commonly used methods, as discussed in the W3Schools guide.

    Reading Integers

    To read an integer input, use the nextInt() method:

       int x = myObj.nextInt();
    

    This method reads the next integer from the input stream and stores it in the variable x.

    Reading Doubles

    To read a double (decimal number) input, use the nextDouble() method:

       double x = myObj.nextDouble();
    

    This method reads the next double from the input stream and stores it in the variable x.

    Reading Strings

    To read a string input, use the nextLine() method:

       String x = myObj.nextLine();
    

    This method reads the next line of text from the input stream and stores it in the variable x.

    Closing the Scanner

    After you're done using the Scanner, it's essential to close it to free up system resources. This can be done using the close() method:

       myObj.close();
    

    Failing to close the Scanner may lead to resource leaks and potential issues in your application.

    As your understanding of the Java Scanner class grows, you'll see how invaluable it is for input/output operations in your applications. By following the best practices and guidelines discussed in this article, along with those found on W3Schools, you can harness the full power of the Scanner class.

    Now that you're equipped with the knowledge of using the Java Scanner class, why not put this newfound understanding into practice? Start building Java applications that read user inputs or process data from files using the Scanner class. Happy coding!