×

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

Related Topics

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Relatively Prime in Java

In this article, you will be very well equipped with a knowledge of a relatively prime number and also Java programs to determine whether a given integer is a relatively...

4 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Java Type Casting

Type casting is a technique or process used in Java to convert one data type into another, either manually or automatically. The compiler performs the automatic conversion, and the programmer...

3 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

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

4 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

String Handling Method in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Polymorphism Program in Java

Polymorphism Program in Java: Polymorphism means the existence of different forms of an object. The object can be a class object or a real-time entity. For example, a person can...

5 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.