Java Scanner

 Java Scanner

  • Scanner class in Java is found in the java.util package. 
  • Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.
  • The Java Scanner class is widely used to parse text for strings and primitive types using a regular expression. 
  • It is the simplest way to get input in Java. 
  • By the help of Scanner in Java, we can get input from the user in primitive types such as int, long, double, byte, float, short, etc.

How to create object in program of scanner class:-

        Scanner obj= new Scanner (System.in)

        Standard input-System.in

          Demo d=new demo()

Input Types:-

Method

Description

nextBoolean()

Reads a boolean value from the user

nextByte()

Reads a byte value from the user

nextDouble()

Reads a double value from the user

nextFloat()

Reads a float value from the user

nextInt()

Reads a int value from the user

nextLine()

Reads a String value from the user

nextLong()

Reads a long value from the user

nextShort()

Reads a short value from the user

 

Java Scanner Class Constructors:-

SN

Constructor

Description

1)

Scanner(File source)

It constructs a new Scanner that produces values scanned from the specified file.

2)

Scanner(File source, String charsetName)

It constructs a new Scanner that produces values scanned from the specified file.

3)

Scanner(InputStream source)

It constructs a new Scanner that produces values scanned from the specified input stream.

4)

Scanner(InputStream source, String charsetName)

It constructs a new Scanner that produces values scanned from the specified input stream.

5)

Scanner(Readable source)

It constructs a new Scanner that produces values scanned from the specified source.

6)

Scanner(String source)

It constructs a new Scanner that produces values scanned from the specified string.

7)

Scanner(ReadableByteChannel source)

It constructs a new Scanner that produces values scanned from the specified channel.

8)

Scanner(ReadableByteChannel source, String charsetName)

It constructs a new Scanner that produces values scanned from the specified channel.

9)

Scanner(Path source)

It constructs a new Scanner that produces values scanned from the specified file.

10)

Scanner(Path source, String charsetName)

It constructs a new Scanner that produces values scanned from the specified file.

 

Example 1:-

import java .util.Scanner;

class Test

{

    Public static void main(String args[])

    {

      int a;

    Scanner obj =new Scanner (System.in);

    System.out.println(“Enter a no”);

    a=obj.nextInt();

    System.out.println(“cube of ”,+(a*a*a));

    }

}

Output:-

Enter a no 2

cube of 8


Example 2:- 

import java.util.*;  
public class Example {  
public static void main(String args[])
{  
          Scanner in = new Scanner(System.in);  
          System.out.print("Enter your name: ");  
          String name = in.nextLine();  
          System.out.println("Name is: " + name);             
          in.close();             
          }  
}  
 Output:-
Enter your name: sonoo jaiswal
Name is: sonoo jaiswal



Comments

Popular posts from this blog

Java Package

Life cycle of a Thread and creating thread class

Inheritance in Java