Scanner in Java
Static way of Programming:
When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a value.
Program:
Import java.io.*;
class A
{
public static void main(String s[])
{
int var1=1;
// Here “var1” assigned to 1.
System. out. print ln (“var1 = “+var1);
}
}
Input:
1
Output:
var1=1
Note: The above program is about considering static values of variables in programming.
To access input from the user or to access dynamic values of variables from the keyboard we use the Scanner class or BufferReader class.
Scanner class:
- It is used to read input from the keyboard.
- java. Util package contains Scanner class.
- We should create an object for the scanner class.
The syntax for the Creation of an Object:
Classname objectname = new Classname();
To access variables of instance variables or instance methods we should create classes.
new: It is used to allocate memory in the heap area.
Constructor:
Classname() is a constructor used to initialize the instance variables before creating objects. If a constructor is not available in class then the java compiler provides the default constructor.
Note: These created objects are allocated memory in the heap area.
The object for Scanner:
- Scanner object -new Scanner()
- Scanner class – Classname.
- Object – object name for the created object.
- Scanner() – Constructor.
To use the scanner class in java. We have to import the package java. util.
The syntax for Importing Package:
import package_name;
For example,
import java. util.Scanner;
Note: The above line of code indicates java. util package providing scanner class in the program.
For example,
import java. util.*;
Note: The above line of code indicates java. util.* package providing all classes and methods in the program.
Methods to take Input:
- nextByte()
- nextShort()
- nextInt()
- nextLong()
- nextFloat()
- nextDouble()
- nextBoolean()
- next()
- nextLine()
nextByte():
- It is used to read byte values from keyboard or user.
- Size of byte is 8 bits or 1 byte.
- Byte value range lies between -2^7 to 2^7 -1;
Program:
import java.io.*;
import java.util.*;
Class B
{
public static void main(String s[])
{
Scanner obj1 = new Scanner(System.in);
Byte var1= obj1.nextByte();
System.out.prinln(“var1=”+var1);
}
}
Input:
2
Output:
Var1=2
Note: If the value is more than 127 or less than -128 then it shows an error-like value out of range.
next short():
- It is used to read short values from the keyboard.
- Size of short is 2 bytes.
- Short value range lies between -2^15 to 2^15 -1.
Program:
import java.io.*;
import java.util.*;
class C
{
public static void main(String s[])
{
Scanner obj1 = new Scanner(System.in);
short a=obj1.nextShort();
}
}
Input:
345
Output:
a=345
nextInt():
- It is used to read Integer values from the keyboard.
- Size of Integer is 2 bytes.
- Integer value range lies between -2^31 to 2^31 -1.
Program:
import java.io.*;
import java.util.*;
class C
{
public static void main(String args[])
{
Scanner obj1= new Scanner();
Int a = obj1.nextInt();
System . out . println(“a=”+a);
}
}
Input:
567
Output:
a=567
nextLong():
- It is used to read long values from the keyboard.
- Size of long is 8 bytes.
- Long value range lies between 2^63 to 2^63 -1.
Program:
import java.io.*;
import java.util.*;
class D
{
public static void main(String s[])
{
Scanner obj1=new Scanner(System.in);
long var1=sc.nextLong();
System.out.println(“var1=”+var1);
}
}
Input:
10888888898
Output:
Var1=108888898
nextFloat():
- It is used to read float values from the keyboard.
- Size of the float is 4 bytes.
Program:
import java.io.*;
import java.util.*;
class E
{
public static void main(String s[])
{
Scanner obj1= new Scanner(System.in);
float var1=obj1.nextFloat();
System.out.println(“var1=”+a);
}
}
Input:
2.55
Output:
Var1=2.55
nextDouble():
- It is used to read double values from the user.
- Size of double is 8 bytes.
Program:
import java.io.*;
import java.util.*;
class F
{
public static void main(String s[])
{
Scanner obj1 = new Scanner(System.in);
Double var1 = obj1.nextDouble();
System.out.println(“var1=”+var1);
}
}
Input:
34.55
Output:
Var1=34.55
nextBoolean(): It is used to read Boolean values from the keyboard i.e, true or false.Size of Boolean is 1 bit.
Program:
import java.io.*;
import java.util.*;
class G
{
public static void main(String s[])
{
Scanner obj1 = new Scanner(System.in);
boolean flag=obj1.nextBoolean();
System.out.println(“flag=”+aflag):
}
}
Input:
true
Output:
flag=true
next(): It is used to read strings from the keyboard.
Program:
import java.io.*;
import java.util.*;
class Str
{
public static void main(String s[])
{
Scanner obj1= new Scanner(System.in);
String s=obj1.next();
System.out.println(“s=”+s);
}
}
Input:
Java
Output:
S=Java
nextLine(): It is used to read an entire line from the keyboard.
Program:
import java.io.*;
import java.util.*;
class Str1
{
public static void main(String s[])
{
Scanner obj1 = new Scanner(System.in);
String s=obj1.nextLine();
System.out.println(“s=”+s);
}
}
Input:
Java Programming
Output:
s=Java Programming