Alright, let's elaborate with some simplified explanation about the Scanner class.

It is a standard Oracle class which you can use by calling the import java.util.Scanner.

So let's make a basic example of the class:

class Scanner {
   InputStream source;

   Scanner(InputStream src) {
       this.source = src;
   }

   int nextInt() {
       int nextInteger;
       //Scans the next token of the input as an int from the source.
       return nextInteger;
   }
}

Now when you call Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input. At the same time you are calling the (so called) constructor of the class, with the parameter System.in. That means it is going to read from the standard input stream of the program.

Now when you are calling input.nextInt(); you execute the method from the object you just created (also documented). But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do:

int i = input.nextInt();
Answer from moffeltje on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_user_input.asp
Java User Input (Scanner class)
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The Scanner class is used to get user input, and it is found in the ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Scanner.html
Scanner (Java Platform SE 8 )
April 21, 2026 - Constructs a new Scanner that produces values scanned from the specified file. Bytes from the file are converted into characters using the underlying platform's default charset. ... Constructs a new Scanner that produces values scanned from the specified file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › scanner-class-in-java
Scanner Class in Java - GeeksforGeeks
Scanner is a predefined class in Java used to take input from the user. It is present in the java.util package and supports different types of input such as integers, strings, float values, and characters.
Published   May 27, 2026
🌐
Programiz
programiz.com › java-programming › scanner
Java Scanner (With Examples)
Scanner input = new Scanner(System.in); Here, we have created an object of Scanner named input. The System.in parameter is used to take input from the standard input. It works just like taking inputs from the keyboard. We have then used the nextLine() method of the Scanner class to read a line ...
🌐
Zero To Mastery
zerotomastery.io › blog › java-scanner
Beginner's Guide To Java Scanner (With Code Examples) | Zero To Mastery
This tells Java, "I want to use the Scanner class in this program." Without this import, trying to use Scanner would result in a compilation error. Once you've imported Scanner, you need to create a Scanner object to actually start reading input. The most common way to do this is by linking it to System.in, which represents input from the keyboard: ... This line creates a new Scanner object and connects it to the keyboard, allowing your program to accept user input.
🌐
Android Developers
developer.android.com › android studio › run apps on a hardware device
Run apps on a hardware device | Android Studio | Android Developers
4 weeks ago - Connect your device over USB: The Connection Assistant first prompts you to connect your device over USB and provides a Rescan USB devices button where you can start a new scan for connected devices.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › how to use 'scanner' in java?
r/learnprogramming on Reddit: How to use 'scanner' in JAVA?
September 18, 2022 -

I have to learn how to code in JAVA for school, I have been trying for many hours now but I fail to understand how to to use 'Scanner'

Could anybody help me out with this code to at least teach me how I can use it right the next time? Because the answers to my mistakes is nowhere to be found even after watching countless of Youtube videos.

I have to change the code so that the number will get printed.

Sample input1: 12 > The sample output for this has to be also: 12

Sample input 2: 42 > The sample output for this also has to be: 42

Sample imput 3: -12 > The sample output for this also has to be: -12

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = New Scanner(System.in);

int getal = scanner.nextInt();

System.out.println(........)

And then I honestly have no clue what to do..

Please help and thank you in advance

🌐
Baeldung
baeldung.com › home › java › java io › java scanner
Java Scanner | Baeldung
January 5, 2024 - A quick and practical set of examples for using the core Scanner Class in Java - to work with Strings, Files and user input.
🌐
Coderanch
coderanch.com › t › 775648 › java › scanner-input
scanner input (Beginning Java forum at Coderanch)
July 30, 2023 - Where dose it go in the code above ... i fixed my problem by 1.placing the scanner outside the main method below the class name. public static final Scanner sc = new Scanner( System.in ); 2....
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-import
Java Scanner import
In contrast, when an explicit Java Scanner import is performed with the import java.util.Scanner; statement, only the Scanner class becomes available to your code. To use other classes in the java.util package, you must add explicit imports of those classes. For the sake of simplicity, I recommend new developers use the wildcard approach wto import the Java Scanner class.
🌐
Udemy
blog.udemy.com › home › it & development › software development › java scanner : basic implementation of the scanner class
Java Scanner : Basic Implementation of the Scanner Class - Udemy Blog
April 14, 2026 - Scanner scant = new Scanner(System.in); System.out.println(scant.nextInt()); if (scant.hasNextInt()) // Checks if next line has integer input System.out.println(scant.nextInt()); ... import java.io.*; import java.util.Scanner; public class ...
🌐
Lime
li.me › homepage
E-scooter & E-bike Sharing | Lime Micromobility
January 21, 2026 - Go car-free with the world’s largest shared electric vehicle company. Lime is on a mission to build a future where transportation is shared, affordable…
🌐
Gitbook
yubin551.gitbook.io › java-note › basic_java_programming › standard_input_scanner
標準輸入 Scanner | Java備忘筆記
November 6, 2020 - Scanner s = new Scanner(System.in); Copy · import java.util.Scanner; public class Test { public static void main(String[] args){ Scanner s=new Scanner(System.in); System.out.println("你好我是JP,請問你的名字是?"); String name ...
🌐
Canon Global
global.canon › en
Canon Global
April 22, 2026 - Through continuous innovation across its four business segments — Printing, Medical, Imaging, and Industrial — Canon creates new possibilities for customers worldwide.
🌐
Amazon Web Services
aws.amazon.com › generative ai › amazon q › amazon q developer
Coding Assistant - Amazon Q Developer - AWS
4 days ago - Accelerate software development with Amazon Q Developer — an AI coding assistant that writes, debugs, and refactors code natively in your IDE.
🌐
OneCompiler
onecompiler.com › java
Java Online Compiler
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter your name: "); String inp = input.next(); System.out.println("Hello, " + inp); } }
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-User-Input-example-String-next-int-long-char
Java Scanner User Input Example
The Java Scanner class is a simple, versatile, easy-to-use class that makes user input in Java relatively straightforward. To perform user input with the Scanner class, follow these steps: Create an instance of the Scanner with the new keyword.
🌐
W3Schools
w3schools.com › java › java_ref_scanner.asp
Java Scanner Class Reference
Java Examples Java Videos Java ... Java Syllabus Java Study Plan Java Interview Q&A ... The Scanner class can be used to obtain data from the keyboard, files and strings....