Scanner class

Java Scanner Class in Java

Scanner is one of the predefined class which is used for reading the data dynamically from the keyboard.
Import Scanner class

Import Scanner Class in Java

java.util.Scanner

Constructor of Scanner Class

Scanner(InputStream)

This constructor create an object of Scanner class by talking an object of InputStream class. An object of InputStream class is called in which is created as a static data member in the System class.

Syntax of Scanner Class in Java

Scanner  sc=new  Scanner(System.in);
Here the object 'in' is use the control of keyboard
scanner class in javascanner class in java

Instance methods of Scanner Class

MethodDescription
1public byte nextByte()Used for read byte value
2public short nextShort()Used for read short value
3public int nextInt()Used for read integer value
4public long nextLong()Used for read numeric value
5public float nextLong()Used for read numeric value
6public double nextDouble()Used for read double value
7public char nextChar()Used for read character
8public boolean nextBoolean()Used for read boolean value
9public String nextLine()Used for reading any kind of data in the form of String data.
Method 1 to 8 are used for reading fundamental values from the keyboard. Method 9 (public String nextLine() ) is used for reading any kind of data in the form of String data.

For Remember all above methods

From method 1 to 8 combindly we represent as public xxx nextxxx(). Here xxx represents any fundamental data type. These methods are used for reading the fundamental data from keyboard.

Accept two values dynamically from the keyboard and compute sum.

Example of Scanner Class in Java

import java.util.Scanner
public class ScannerDemo
{
public static void main(String args[])
{
Scanner  s=new  Scanner(System.in);
System.out.println("Enter first no= ");
int num1=s.nextInt();
System.out.println("Enter second no= ");
int num2=s.nextInt();
System.out.println("Sum of no is= "+(num1+num2));
}
}

Output

Enter first no=4
Enter second no=5
Sum of no is=9

Program which is accept two number as a string and compute their sum.

Example

import java.util.Scanner;
class Dataread 
{
public static void main(String[] args) 
{
Scanner s=new Scanner(System.in);
System.out.println("Enter first number: ");
String s1=s.nextLine();
System.out.println("Enter second number: ");
String s2=s.nextLine();
int res=Integer.parseInt(s1) + Integer.parseInt(s2);
System.out.println("Sum= "+res);
}
}

Output

Enter first number: 5
Enter second number: 6
Sum= 11
// Java program to read data of various types using Scanner class.
import java.util.Scanner;
public class ScannerDemo1
{
    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);
 
        // Numerical data input
        // byte, short and float can be read
        // using similar-named functions.
        int age = sc.nextInt();
        long mobileNo = sc.nextLong();
        double cgpa = sc.nextDouble();
 
        // Print the values to check if input was correctly obtained.
        System.out.println("Name: "+name);
        System.out.println("Gender: "+gender);
        System.out.println("Age: "+age);
        System.out.println("Mobile Number: "+mobileNo);
        System.out.println("CGPA: "+cgpa);
    }
}
Input :
Geek
F
40
9876543210
9.9
Output :
Name: Geek
Gender: F
Age: 40
Mobile Number: 9876543210
CGPA: 9.9

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...