What is Scanner class?
Scanner is a class used for obtaining the input of the primitive types like int, double etc. and strings.
To use the Scanner class, we first need to include java.util package in our program.
We include a package in a program with the help of import keyword. We can either import the java.util.Scanner class or the entire java.util package.
To import a class or a package, add one of the following lines to the very beginning of your code.
Example:
import java.util.Scanner; // This will import just the Scanner class import java.util.*; // This will import the entire java.util package
After importing, we need to write the following statement in our program.
Example:
Scanner s = new Scanner (System.in);
Here by writing Scanner s, we are declaring s as an object of Scanner class.
System.in within the round brackets tells Java that this will be System Input i.e. input will be given to the system.
Methods in Scanner Class:
Method | Inputs |
---|---|
nextInt() | Integer |
nextFloat() | Float |
nextDouble() | Double |
nextLong() | Long |
nextShort() | Short |
next() | Single word |
nextLine() | Line of Strings |
nextBoolean() | Boolean |
How to input a Integer value?
we will input an integer value from the user using nextInt() method.
Example:
import java.util.Scanner; class IntegerDemo{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter an integer"); int a; a = s1.nextInt(); System.out.println("The entered integer is" + a); } }
Output:
Enter an integer 14 The entered integer is14
How to input a word?
we will input an word from the user using next() method.
Example:
import java.util.*; class WordDemo{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter your first name"); String f = s1.next(); System.out.println("Enter your last name"); String l = s1.next(); System.out.println("Your name is " + f + l); } }
Output:
Enter your first name Robert Enter your last name Jhon Your name is RobertJhon
How to input a line or Sentence?
we will input a line or sentence from the user using nextLine() method.
Example:
import java.util.*; class SentenceDemo{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter your name"); String name = s1.nextLine(); System.out.println("Your name is " + name); } }
Output:
Enter your name Robert Jhon Your name is Robert Jhon
The only difference between the methods nextLine() and next() is that nextLine() reads the entire line including white spaces, whereas next() reads only upto the first white space and then stops.
Example:
import java.util.*; class DifferenceDemo{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter your name"); String w = s1.next(); System.out.println("Your name is " + w); System.out.println("Again enter your name"); String st = s1.nextLine(); System.out.println("Your name is " + st); } }
Output:
Enter your name Robert Brown Jr. Your name is Robert Again enter your name Your name is Brown Jr.
Here, next took only the first word. Rest of the words were taken by nextLine.
How to input different datatypes?
Above table shows different methods to get the input like integer,double float,string.etc.,
Example:
import java.util.*; class DifferentDataDemo{ public static void main(String[] args){ Scanner s1 = new Scanner(System.in); System.out.println("Enter integer"); int n = s1.nextInt(); System.out.println("Enter double"); double db = s1.nextDouble(); System.out.println("Integer :" + n); System.out.println("Double :" + db); } }
Output:
Enter integer 45 Enter double 23.242 Integer :45 Double :23.242
Solve this!!
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling