Scanner Class in Java


Prev Tutorial Next Tutorial

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


Constructor of Scanner Class


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(;

Here the object 'in' is use the control of keyboard

scanner class in java scanner class in java

Instance methods of Scanner Class

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.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));


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.


import java.util.Scanner;
class Dataread 
public static void main(String[] args) 
Scanner s=new Scanner(;
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);


Enter first number: 5
Enter second number: 6
Sum= 11

Prev Tutorial Next Tutorial


Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements

Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: or 9999595223 Try this Keyword C++ Programs