SlideShare a Scribd company logo
Reading Input
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
Java primarily developed for Graphical User Interface, for this
reason Java doesn’t have explicit statement like scanf(). But however
we can read input into java program using the following ways.
1.Using System.in.read(): reads only one char in form of unicode
2.Using Stream classes (defined in java.io): very flexible and lot of
features provided. But they are bit complex and needs experience in
writing the programs
3.Using Scanner class (defined in java.util): very easy to use. Added
after jdk 1.5.0 only.
SSBN Degree College, ATP M Vishnuvardhan
How to read data using Scanner
The following steps are to be taken in order to read data
from Scanner class
1.Import util package (import java.util.*;)
2.Create an object to Scanner class
Scanner objName=new Scanner (Source);
1.Use appropriate method to read required data
SSBN Degree College, ATP M Vishnuvardhan
What is a source
Means from where the data is read in to the program. The source can
be keyboard, File on HDD, network computer, internet …
E.g: Scanner s=new Scanner (System.in);
Scanner s= new Scanner (new File(“D:Sample.txt”));
Source ProgramReading
Keyboard – System.in
File - FileReader or FileInputStream or File
Network – Socket
SSBN Degree College, ATP M Vishnuvardhan
Scanner methods
Scanner defines a rich set of methods
General Syntax
byte nextByte():
short nextShort():
int nextInt():
long nextLong():
Note: no explicit method to read single char in java
(i.e., there is no nextChar() )
 float nextFloat():
 double nextDouble():
 boolean nextBoolean():
 String next():
 String nextLine():
xxx nextXxx():
SSBN Degree College, ATP M Vishnuvardhan
Questions
SSBN Degree College, ATP M Vishnuvardhan
Questions

More Related Content

What's hot

String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
Ravi_Kant_Sahu
 

What's hot (20)

Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java IO
Java IOJava IO
Java IO
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
The Loops
The LoopsThe Loops
The Loops
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 

Similar to Scanner class

Java programming questions1. Why do we instantiate an object of a .pdf
Java programming questions1. Why do we instantiate an object of a .pdfJava programming questions1. Why do we instantiate an object of a .pdf
Java programming questions1. Why do we instantiate an object of a .pdf
arrowmobile
 
66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
Laura Popovici
 
Class IX Input in Java using Scanner Class (1).pptx
Class IX  Input in Java using Scanner Class (1).pptxClass IX  Input in Java using Scanner Class (1).pptx
Class IX Input in Java using Scanner Class (1).pptx
rinkugupta37
 

Similar to Scanner class (20)

Java programming questions1. Why do we instantiate an object of a .pdf
Java programming questions1. Why do we instantiate an object of a .pdfJava programming questions1. Why do we instantiate an object of a .pdf
Java programming questions1. Why do we instantiate an object of a .pdf
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
 
66781291 java-lab-manual
66781291 java-lab-manual66781291 java-lab-manual
66781291 java-lab-manual
 
Java
JavaJava
Java
 
Java programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, MysuruJava programming material for beginners by Nithin, VVCE, Mysuru
Java programming material for beginners by Nithin, VVCE, Mysuru
 
Programming in Java
Programming in JavaProgramming in Java
Programming in Java
 
Spring notes
Spring notesSpring notes
Spring notes
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
CORE JAVA
CORE JAVACORE JAVA
CORE JAVA
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
 
Introduction
IntroductionIntroduction
Introduction
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptx
 
Spring Basics
Spring BasicsSpring Basics
Spring Basics
 
Class IX Input in Java using Scanner Class (1).pptx
Class IX  Input in Java using Scanner Class (1).pptxClass IX  Input in Java using Scanner Class (1).pptx
Class IX Input in Java using Scanner Class (1).pptx
 
Core JAVA presentation for 1st year students
Core JAVA presentation for 1st year studentsCore JAVA presentation for 1st year students
Core JAVA presentation for 1st year students
 
Java intro
Java introJava intro
Java intro
 
JAVA Program Examples
JAVA Program ExamplesJAVA Program Examples
JAVA Program Examples
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 

More from M Vishnuvardhan Reddy

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java applets
Java appletsJava applets
Java applets
 
Exception handling
Exception handling Exception handling
Exception handling
 
Control structures
Control structuresControl structures
Control structures
 
Constructors
ConstructorsConstructors
Constructors
 
Shell sort
Shell sortShell sort
Shell sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 

Recently uploaded

Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 

Recently uploaded (20)

Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 

Scanner class

  • 1. Reading Input In JAVA -M Vishnuvardhan, Dept. of Computer Science, SSBN Degree College, ATP
  • 2. SSBN Degree College, ATP M Vishnuvardhan Introduction Java primarily developed for Graphical User Interface, for this reason Java doesn’t have explicit statement like scanf(). But however we can read input into java program using the following ways. 1.Using System.in.read(): reads only one char in form of unicode 2.Using Stream classes (defined in java.io): very flexible and lot of features provided. But they are bit complex and needs experience in writing the programs 3.Using Scanner class (defined in java.util): very easy to use. Added after jdk 1.5.0 only.
  • 3. SSBN Degree College, ATP M Vishnuvardhan How to read data using Scanner The following steps are to be taken in order to read data from Scanner class 1.Import util package (import java.util.*;) 2.Create an object to Scanner class Scanner objName=new Scanner (Source); 1.Use appropriate method to read required data
  • 4. SSBN Degree College, ATP M Vishnuvardhan What is a source Means from where the data is read in to the program. The source can be keyboard, File on HDD, network computer, internet … E.g: Scanner s=new Scanner (System.in); Scanner s= new Scanner (new File(“D:Sample.txt”)); Source ProgramReading Keyboard – System.in File - FileReader or FileInputStream or File Network – Socket
  • 5. SSBN Degree College, ATP M Vishnuvardhan Scanner methods Scanner defines a rich set of methods General Syntax byte nextByte(): short nextShort(): int nextInt(): long nextLong(): Note: no explicit method to read single char in java (i.e., there is no nextChar() )  float nextFloat():  double nextDouble():  boolean nextBoolean():  String next():  String nextLine(): xxx nextXxx():
  • 6. SSBN Degree College, ATP M Vishnuvardhan Questions
  • 7. SSBN Degree College, ATP M Vishnuvardhan Questions