Scanner is a class. By using the “new” operator, you are creating an object from that class. The parentheses indicate that you are calling a method (in this case, the constructor, which is why its name is same as class. The System.in is the parameter passed to the constructor, in this case telling Scanner where it is scanning from (System.in is the keyboard). So it’s telling the Scanner object to look for input from the keyboard. Basically, this is how you can read something a user types, like answer to a yes/no question, or the number pressed from a numbered menu, etc… Answer from kingtermite on reddit.com
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Scanner.html
Scanner (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
🌐
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 ...
Discussions

What exactly is Scanner??
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
22
1
October 31, 2023
Fundamentally misunderstanding Java Scanner class.
It seems the consensus is that each program only requires a single Scanner object. There is no such consensus. If you need to read from multiple sources you can and should use several scanners. Secondly, what actually does the close() method of Scanner do? It does whatever it needs to do to properly close the underlying source. It can be a bunch of different things or nothing at all. In the case of a file, it signals the OS that you are done reading so it can free any resource associated with the file. For something like a network connection it might be more complicated. Is the file actually being "opened" somewhere? Yes, files are opened. Scanner probably opens it when it is created. Opening a file tells the OS that you are gonna read (or write) from said file, so it can do the necessary work to make the data available to you. Can Scanners be "re-opened"? I don't believe so, once closed it is basically useless. should you just be closing Scanners (particularly, Scanners with files) and the end of each program? The OS will close any file your program has opened when it is closed. So you don't need to close every scanner. It is good practice to do so tho, since some things do not like to be abruptly closed by the OS (files are fine). And lastly, what's even the purpose of the Scanner class at all? Why do we need a class at all to get input? Does Java not have a Console.ReadLine() equivalent that we can just set to a String variable? Scanner is a convenience class to help read text input from different sources. If you only ever read string from the console you don't need it, but many program do more than that. More on reddit.com
🌐 r/AskProgramming
10
6
April 29, 2021
A small rant regarding the Scanner class...
You rarely see Scanner used anywhere else but with tutorials and class projects. Maybe if you're building a cli tool, but otherwise it's rarely used, which is probably why you probably won't find a huge amount of info online about it's usage. I'm guessing you've read through the JavaDocs for the Class? More on reddit.com
🌐 r/learnjava
22
29
May 14, 2022
Why do we need Scanner class in order to input something?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnjava
8
9
July 11, 2022
People also ask

Q1. What is the Scanner class in Java?
The Scanner class is a utility in Java that helps programs read input from the user, files, or strings.
🌐
intellipaat.com
intellipaat.com › home › blog › scanner class in java
Scanner Class in Java: Syntax, Methods, and Examples
Q2. What type of class is the Scanner class?
Scanner is a built-in class in java.util package.
🌐
intellipaat.com
intellipaat.com › home › blog › scanner class in java
Scanner Class in Java: Syntax, Methods, and Examples
Q3. How to write a Scanner class?
You create a Scanner object in your program using: Scanner sc = new Scanner(System.in);
🌐
intellipaat.com
intellipaat.com › home › blog › scanner class in java
Scanner Class in Java: Syntax, Methods, and Examples
🌐
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
🌐
Zero To Mastery
zerotomastery.io › blog › java-scanner
Beginner's Guide To Java Scanner (With Code Examples) | Zero To Mastery
Java organizes its built-in tools into packages, and Scanner belongs to the java.util package. To use it, you need to import it at the beginning of your program: ... 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.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.util.scanner
Scanner Class (Java.Util) | Microsoft Learn
For example, the pattern "\\s+" will return no empty tokens since it matches multiple instances of the delimiter. The delimiting pattern "\\s" could return empty tokens since it only passes one space at a time. A scanner can read text from any object which implements the java.lang.Readable ...
Find elsewhere
🌐
Medium
medium.com › buzz-code › java-8-scanner-class-660b03d4bb91
Java 8 | Scanner Class
January 18, 2021 - So with the Scanner class you can type something in from the console.
🌐
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
Found in the java.util package, Java’s Scanner class can read input from the command line and return it as a String, BigDecimal or any one of Java’s eight primitive types.
🌐
Reddit
reddit.com › r/askprogramming › fundamentally misunderstanding java scanner class.
r/AskProgramming on Reddit: Fundamentally misunderstanding Java Scanner class.
April 29, 2021 -

Hello! My wife is taking a beginner programming class in Java, and she's having trouble with Files and Scanners. In my quest to help her understand it, I realized that I don't really understand it myself.

For example: It seems the consensus is that each program only requires a single Scanner object. But if that's the case, how do you handle when a program needs to take input from multiple sources? Say a program needs to take a user prompt in the console, but you'd also like to use the Scanner class on a File object to easily loop through the file and use methods like nextLine(). How would you handle that with only a single Scanner?

Secondly, what actually does the close() method of Scanner do? My wife seems to think that using a line like Scanner inputFile = new Scanner(f) "opens" the file, and close "closes" the file, and we use close because it's the responsible thing to do. But the opening and closing a file makes sense on an OS to me- you're opening it in a program, starting a new task, and when you close it, you end that task. Is the file actually being "opened" somewhere? I'm guessing not. And if not, what does "closing" it actually mean? (I googled that for a while and could not fine a decent answer. The documentation just says "it closes the Scanner"). And you can close system. in , so how does that relate to all of this? Can Scanners be "re-opened"? Is that even useful, or should you just be closing Scanners (particularly, Scanners with files) and the end of each program?

And lastly, what's even the purpose of the Scanner class at all? Why do we need a class at all to get input? Does Java not have a Console.ReadLine() equivalent that we can just set to a String variable?

Sorry, I know it's a lot of questions, but I just like to understand these things as much as I can so I don't spread misinformation or use a bad analogy. Thanks for any help in advance.

Top answer
1 of 6
7
It seems the consensus is that each program only requires a single Scanner object. There is no such consensus. If you need to read from multiple sources you can and should use several scanners. Secondly, what actually does the close() method of Scanner do? It does whatever it needs to do to properly close the underlying source. It can be a bunch of different things or nothing at all. In the case of a file, it signals the OS that you are done reading so it can free any resource associated with the file. For something like a network connection it might be more complicated. Is the file actually being "opened" somewhere? Yes, files are opened. Scanner probably opens it when it is created. Opening a file tells the OS that you are gonna read (or write) from said file, so it can do the necessary work to make the data available to you. Can Scanners be "re-opened"? I don't believe so, once closed it is basically useless. should you just be closing Scanners (particularly, Scanners with files) and the end of each program? The OS will close any file your program has opened when it is closed. So you don't need to close every scanner. It is good practice to do so tho, since some things do not like to be abruptly closed by the OS (files are fine). And lastly, what's even the purpose of the Scanner class at all? Why do we need a class at all to get input? Does Java not have a Console.ReadLine() equivalent that we can just set to a String variable? Scanner is a convenience class to help read text input from different sources. If you only ever read string from the console you don't need it, but many program do more than that.
2 of 6
3
It seems the consensus is that each program only requires a single Scanner object. This isn't true. You would want a single Scanner object per source. Having multiple Scanners all reading from standard input would cause unpredictable output. Sometimes teachers simplify things for the moment. If the only thing you're reading from is standard in, you would only want a single Scanner object in your program. what does "closing" it actually mean? This is a fairly common pattern in Java. You don't really need to know what it means, other than there is some resource it allocates that the close immediately releases. In this case, the resource is likely buffers and/or a file handle. Can Scanners be "re-opened"? No. You would just create a new Scanner if you needed it again. Generally, you would keep it open as long as you need, instead of creating new ones all the time, so you don't lose buffers and such that are speeding things up for you. what's even the purpose of the Scanner class at all? Java was designed with flexibility in mind over simplicity. One of the nice things about having a separate Scanner is you can use it with anything implementing the Readable interface. This is really nice when you're writing automated tests. Personally, I think they took it a bit too far, but it is a pretty big step up from the C standard library.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-String-input-with-the-Scanner-class
Java Scanner String input example
July 23, 2025 - –Dell Technologies ... import java.util.Scanner; public class ScannerUserInput { public static void main(String[] args) { // String input with the Java Scanner System.out.println("How old are you?"); Scanner stringScanner = new Scanner(System.in); ...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Java-Scanner-import
Java Scanner import
The java.util.Scanner class is one of the first components that new Java developers encounter. To use it in your code, you should import it, although another option is to explicitly reference the package in your code.
🌐
CodeGym
codegym.cc › java blog › java classes › java scanner class
Scanner Class in Java | CodeGym
April 1, 2025 - First and foremost, we must get acquainted with the java.util.Scanner class. Like a real scanner, it reads data from a source that you specify (for example, a string, a file, the console). Next, it identifies the information and processes it ...
🌐
Medium
medium.com › @payalmehra3522 › day-31-of-learning-java-understanding-the-scanner-class-in-java-eabdde4d4a05
Day 31 of Learning Java: Understanding the Scanner Class in Java | by Payal Mehra | Medium
March 12, 2026 - The Scanner class allows us to read input from different sources like keyboard, files, or strings. Most commonly, it is used to take input from the keyboard. The Scanner class is part of the java.util package and is used to read input data from ...
🌐
Career Karma
careerkarma.com › blog › java › java user input and scanner class: a step-by-step guide
Java User Input and Scanner Class: A Step-By-Step Guide | Career Karma
December 1, 2023 - You can collect user input using the Scanner class. The Scanner class reads text that a user inserts into the console and sends that text back to a program. Scanner is the primary method of collecting Java user input.
🌐
W3Schools
w3schools.com › java › java_ref_scanner.asp
Java Scanner Class Reference
Java Examples Java Videos Java ... Java Study Plan Java Interview Q&A ... The Scanner class can be used to obtain data from the keyboard, files and strings....
🌐
DZone
dzone.com › coding › java › if you're using java’s scanner class for keyboard input, you're doing it wrong
If You're Using Java’s Scanner Class for Keyboard Input, You're Doing it Wrong
September 4, 2018 - It never stops to ask for the number and immediately prints out the default value for num. I had never seen this problem before. When I teach an introductory course, I show my students that the Scanner for keyboard input should, in most cases, be a class variable initialized in the constructor.
🌐
Tinocs
java.tinocs.com › lesson › A2 › J.md
The Scanner Class - Tino Intro To Java
January 9, 2026 - The Scanner class is designed to read tokens. Tokens are separated by white space (space, tab, new line ...etc). For example, in "Hi, I am 26 years old.", there are 6 tokens: "Hi,", "I", "am", "26", "years" and "old.". Note that the comma is included in the first token because there is no white ...
🌐
Smartprogramming
smartprogramming.in › tutorials › java › scanner-class
Java Scanner Class
Scanner sc = new Scanner(System.in); char ch = sc.next().charAt(0); To read data from a file, we use Scanner(File file) constructor. Example: import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class ReadFile { public static void main(String[] args) throws ...
🌐
Intellipaat
intellipaat.com › home › blog › scanner class in java
Scanner Class in Java: Syntax, Methods, and Examples
December 15, 2025 - The Scanner class in Java is used to read input from the keyboard, files, or strings. It works by breaking the input from the user into small pieces, which are called tokens. These are usually separated by whitespace.