Emo 3
Emo 3
Emo 3
geeksforgeeks
Courses 90% Refund
Tutorials
Java
Practice
Contests
Sign In
Java Arrays
Java Strings
Java OOPs
Java Collection
Java 8 Tutorial
Java Multithreading
Java Exception Handling
Java Programs
Java Project
Java Collections Interview
Java Interview Questions
Java MCQs
Spring
Spring MVC
Spring Boot
Hibernate
Using the Scanner class in Java is the easiest way to read input in a Java program,
though not very efficient if you want an input method for scenarios where time is a
constraint like in competitive programming.
Method
Description
nextBoolean()
// Driver Class
public class ScannerDemo1 {
// main function
public static void main(String[] args)
{
// Declare the object and initialize with
// predefined standard input object
Scanner sc = new Scanner(System.in);
// String input
String name = sc.nextLine();
// Character input
char gender = sc.next().charAt(0);
Geek
F
40
9876543210
9.9
Output
Name: Geek
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9
Sometimes, we have to check if the next value we read is of a certain type or if
the input has ended (EOF marker encountered).
Then, we check if the scanner’s input is of the type we want with the help of
hasNextXYZ() functions where XYZ is the type we are interested in. The function
returns true if the scanner has a token of that type, otherwise false. For example,
in the below code, we have used hasNextInt(). To check for a string, we use
hasNextLine(). Similarly, to check for a single character, we use
hasNext().charAt(0).
Example 2:
Let us look at the code snippet to read some numbers from the console and print
their mean.
1 2 3 4 5
Output
Mean: 3
Important Points About Java Scanner Class
To create an object of Scanner class, we usually pass the predefined object
System.in, which represents the standard input stream. We may pass an object of
class File if we want to read input from a file.
To read numerical values of a certain data type XYZ, the function to use is
nextXYZ(). For example, to read a value of type short, we can use nextShort()
To read strings, we use nextLine().
To read a single character, we use next().charAt(0). next() function returns the
next token/word in the input as a string and charAt(0) function returns the first
character in that string.
The Scanner class reads an entire line and divides the line into tokens. Tokens are
small elements that have some meaning to the Java compiler. For example, Suppose
there is an input string: How are you
In this case, the scanner object will read the entire line and divides the string
into tokens: “How”, “are” and “you”. The object then iterates over each token and
reads each token using its different methods.
Three 90 Challenge is back on popular demand! After processing refunds worth INR
1CR+, we are back with the offer if you missed it the first time. Get 90% course
fee refund in 90 days. Avail now!
Want to be a master in Backend Development with Java for building robust and
scalable applications? Enroll in Java Backend and Development Live Course by
GeeksforGeeks to get your hands dirty with Backend Programming. Master the key Java
concepts, server-side programming, database integration, and more through hands-on
experiences and live projects. Are you new to Backend development or want to be a
Java Pro? This course equips you with all you need for building high-performance,
heavy-loaded backend systems in Java. Ready to take your Java Backend skills to the
next level? Enroll now and take your development career to sky highs.
Sukrit Bhatnagar
226
Previous Article
How to Take Input From User in Java?
Next Article
Java.io.BufferedReader Class in Java
Read More
Down Arrow
Similar Reads
Why BufferedReader class takes less time for I/O operation than Scanner class in
java
In Java, both BufferReader and Scanner classes can be used for the reading inputs,
but they differ in the performance due to their underlying implementations. The
BufferReader class can be generally faster than the Scanner class because of the
way it handles the input and parsing. This article will guide these difference,
provide the detailed expla
3 min read
Difference Between Scanner and BufferedReader Class in Java
In Java, Scanner and BufferedReader class are sources that serve as ways of reading
inputs. Scanner class is a simple text scanner that can parse primitive types and
strings. It internally uses regular expressions to read different types while on
the other hand BufferedReader class reads text from a character-input stream,
buffering characters so a
3 min read
Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
Java Deprecated API Scanner tool: Java Deprecated API Scanner tool i.e. jdeprscan
is a static analyzing command-line tool which is introduced in JDK 9 for find out
the uses of deprecated API in the given input. Here the input can be .class file
name, directory or JAR file. Whenever we provide any input to jdeprscan command
line tool then it generat
2 min read
Scanner nextFloat() method in Java with Examples
The nextFloat() method of java.util.Scanner class scans the next token of the input
as a Float(). If the translation is successful, the scanner advances past the input
that matched. Syntax: public float nextFloat() Parameters: The function does not
accepts any parameter. Return Value: This function returns the Float scanned from
the input. Exceptio
4 min read
Scanner skip() method in Java with Examples
skip(Pattern pattern) The skip(Pattern pattern) method of java.util.Scanner class
skips input that matches the specified pattern, ignoring the delimiters. The
function skips the input if an anchored match of the specified pattern succeeds it.
Syntax: public Scanner skip(Pattern pattern) Parameters: The function accepts a
mandatory parameter pattern
5 min read
Scanner useRadix() method in Java with Examples
The useRadix(radix) method of java.util.Scanner class sets this scanner's default
radix to the specified radix. A scanner's radix affects elements of its default
number matching regular expressions. Syntax: public Scanner useRadix(int radix)
Parameters: The function accepts a mandatory parameter radix which specifies the
radix to use when scanning
2 min read
Scanner close() method in Java with Examples
The close() method ofjava.util.Scanner class closes the scanner which has been
opened. If the scanner is already closed then on calling this method, it will have
no effect. Syntax: public void close() Return Value: The function does not return
any value. Below programs illustrate the above function: Program 1: // Java program
to illustrate the // c
1 min read
Scanner delimiter() method in Java with Examples
The delimiter() method ofjava.util.Scanner class returns the Pattern this Scanner
is currently using to match delimiters. Syntax: public Pattern delimiter() Return
Value: The function returns the scanner's delimiting pattern. Below programs
illustrate the above function: Program 1: // Java program to illustrate the //
delimiter() method of Scanner
2 min read
Scanner findInLine() method in Java with Examples
findInLine(Pattern pattern) The findInLine(Pattern pattern) method of
java.util.Scanner class tries to find the next occurrence of the specified pattern
ignoring delimiters. If the pattern is found before the next line separator, the
scanner advances past the input that matched and returns the string that matched
the pattern. If no such pattern is
4 min read
Scanner useLocale() method in Java with Examples
The useLocale() method of java.util.Scanner class sets this scanner's locale to the
specified locale. Syntax: public Scanner useLocale(Locale locale) Parameters: The
function accepts a mandatory parameter locale which specifies a string specifying
the locale to use. Return Value: The function returns this modified scanner.
Exceptions: If the radix
2 min read
Article Tags :
Java
School Programming
Java-I/O
Practice Tags :
Java
three90RightbarBannerImg
course-img
215k+ interested Geeks
Master Java Programming - Complete Beginner to Advanced
Avail 90% Refund
course-img
214k+ interested Geeks
JAVA Backend Development - Live
Avail 90% Refund
course-img
30k+ interested Geeks
Manual to Automation Testing: A QA Engineer's Guide
Avail 90% Refund
geeksforgeeks-footer-logo
Corporate & Communications Address:- A-143, 9th Floor, Sovereign Corporate Tower,
Sector- 136, Noida, Uttar Pradesh (201305) | Registered Address:- K 061, Tower K,
Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh,
201305
GFG App on Play Store
GFG App on App Store
Company
About Us
Legal
Careers
In Media
Contact Us
Advertise with us
GFG Corporate Solution
Placement Training Program
Explore
Job-A-Thon Hiring Challenge
Hack-A-Thon
GfG Weekly Contest
Offline Classes (Delhi/NCR)
DSA in JAVA/C++
Master System Design
Master CP
GeeksforGeeks Videos
Geeks Community
Languages
Python
Java
C++
PHP
GoLang
SQL
R Language
Android Tutorial
DSA
Data Structures
Algorithms
DSA for Beginners
Basic DSA Problems
DSA Roadmap
DSA Interview Questions
Competitive Programming
Data Science & ML
Data Science With Python
Data Science For Beginner
Machine Learning Tutorial
ML Maths
Data Visualisation Tutorial
Pandas Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning Tutorial
Web Technologies
HTML
CSS
JavaScript
TypeScript
ReactJS
NextJS
NodeJs
Bootstrap
Tailwind CSS
Python Tutorial
Python Programming Examples
Django Tutorial
Python Projects
Python Tkinter
Web Scraping
OpenCV Tutorial
Python Interview Question
Computer Science
GATE CS Notes
Operating Systems
Computer Network
Database Management System
Software Engineering
Digital Logic Design
Engineering Maths
DevOps
Git
AWS
Docker
Kubernetes
Azure
GCP
DevOps Roadmap
System Design
High Level Design
Low Level Design
UML Diagrams
Interview Guide
Design Patterns
OOAD
System Design Bootcamp
Interview Questions
School Subjects
Mathematics
Physics
Chemistry
Biology
Social Science
English Grammar
Commerce
Accountancy
Business Studies
Economics
Management
HR Management
Finance
Income Tax
Databases
SQL
MYSQL
PostgreSQL
PL/SQL
MongoDB
Preparation Corner
Company-Wise Recruitment Process
Resume Templates
Aptitude Preparation
Puzzles
Company-Wise Preparation
Companies
Colleges
Competitive Exams
JEE Advanced
UGC NET
UPSC
SSC CGL
SBI PO
SBI Clerk
IBPS PO
IBPS Clerk
More Tutorials
Software Development
Software Testing
Product Management
Project Management
Linux
Excel
All Cheat Sheets
Recent Articles
Free Online Tools
Typing Test
Image Editor
Code Formatters
Code Converters
Currency Converter
Random Number Generator
Random Password Generator
Write & Earn
Write an Article
Improve an Article
Pick Topics to Write
Share your Experiences
Internships
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By
using our site, you acknowledge that you have read and understood our Cookie Policy
& Privacy Policy
Got It !
Lightbox