Java Scanner Class

In this tutorial, we will learn about the Scanner class, its methods, their usages, syntaxes, and examples.

Scanner class

It is used to create an object which is used to read data from input stream (keyboard).

Scanner class is defined in java.util package. So, we need to import this packet first before using the methods of Scanner class.

Note: Objects are references variables in Java.

To read input from the Scanner class, we need to import the "java.util" package. The import statement is given below.

import java.util.Scanner;

The Scanner class is used to read data from Standard Input Device, that is keyboard. Now we will create an object Scanner class.

Scanner SC = new Scanner(System.in);

In the above statement, we created the SC object of the Scanner class, here we passed the object System.in to read input from Standard Input Device. The System.in is the reference of Standard Input Device, that is Keyboard.

The Scanner class contains multiple methods to read input for different types of variables from the user.

Constructor

Scanner (object)

Method of Constructor

Scanner object_name  = new Scanner(input_stream_reference);

Example:

Scanner input = new Scanner (System.in);

Here, System.in is the reference of input (keyboard).

List of Scanner Class Methods

Method Syntax Description
close() public void close(); It is used to close this Scanner object when opened otherwise this method does not affect.
delimiter() public Pattern delimiter(); It is used to retrieve the pattern of this Scanner is currently to match delimiters.
hasNextByte() public boolean hasNextByte();
public boolean hasNextByte(int rad);
It is used to check whether this Scanner has next token in its input can be manipulated as a byte in the implicit radix (rad) or not.
hasNextShort() public boolean hasNextShort();
public boolean hasNextShort(int rad);
It is used to check whether this Scanner has next token in its input can be manipulated as a short value in the implicit radix or not.
hasNextInt() public boolean hasNextInt();
public boolean hasNextInt(int rad);
It is used to check whether this Scanner has next token in its input can be manipulated as an int in the implicit radix (rad) or not.
hasNextLong() public boolean hasNextLong();
public boolean hasNextLong(int rad);
It is used to check whether this Scanner has next token in its input can be manipulated as a long in the implicit radix or not.
hasNextBigDecimal() public boolean hasNextBigDecimal(); It is used to check whether the next token in this Scanner input can be manipulated as a BigDecimal or not.
hasNextBoolean() public boolean hasNextBoolean(); It is used to check whether this Scanner's next input can be manipulated as a boolean input or not.
hasNextDouble() public boolean hasNextDouble(); It is used to check whether this Scanner's next input can be manipulated as a double input or not.
hasNextFloat() public boolean hasNextFloat(); It is used to check whether this Scanner's next input can be manipulated as a float input or not.
hasNextBigInteger() public boolean hasNextBigInteger(); It is used to check whether the next token in this Scanner input can be manipulated as a BigInteger or not.
hasNextLine() public boolean hasNextLine(); It is used to check whether any other line in the input of this Scanner exists or not.
hasNext() public boolean hasNext();
public boolean hasNext(Pattern patt);
public boolean hasNext(String patt);
It is used to check whether this Scanner has any other token exists in its input or not.
ioException() public IOException ioException(); It is used to get the IOException when it is thrown by Scanner Readable otherwise it returns null.
locale() public Locale locale(); It is used to return the Locale of this Scanner.
match() public MatchResult match() It is used to get the MatchResult of the last scanning operation operated by this Scanner.
nextByte() byte nextByte() It is used to read a byte value from the user.
nextInt() int nextInt() It is used to read an integer value from user.
nextShort() short nextShort() It is used to read a short integer value from user.
nextLong() long nextLong() It is used to read a long integer value from user.
nextFloat() float nextFloat() It is used to read a 32-bit floating-point number from user.
nextDouble() double nextDouble() It is used to read a 64-bit floating-point number from the user.
nextBigDecimal() public BigDecimal nextBigDecimal() It is used to get the big decimal scanned from the input.
nextBoolean() public boolean nextBoolean() It is used to scans the next token of the input scanned into a boolean value.
next() String next() It is used to read a string number from the user.
radix() public int radix(); It is used to return the default or implicit radix of this Scanner.
remove() public void remove(); It is used to result an exception during the call remove() method.
reset() public Scanner reset(); It is used to reset this Scanner object and it resets a scanner skips all its explicit state information which may have been changed by calling useDelimiter() and useLocale() and useRadix().
toString() public String toString(); It is used to string denotation of this Scanner and it contains information related to tracing.
useLocale() public Scanner useLocale(Locale lo); It is used to use this Scanner locale to the given locale (lo).
useRadix() public Scanner useRadix(int rad); It is used to assigns the default or implicit radix (rad) to the given radix of this Scanner.
skip() public Scanner skip(Pattern patt);
public Scanner skip(String patt);
It is used to skip input that meets the pattern formed from the given string (patt).
useDelimiter() public Scanner skip(Pattern patt);
public Scanner skip(String patt);
It is used to put the delimiter pattern to the given pattern (patt) of this Scanner.
findInLine() public String findInLine(Pattern patt);
public String findInLine(String patt);
It is used to get the string that meets the given pattern (Pattern).
findWithinHorizon() public String findWithinHorizon(Pattern patt, int horiz);
public String findWithinHorizon(String patt, int horiz);
It is used to search the next occurrence of the given Pattern (patt) and it finds through the input up to the given horizon (horiz).

Example 1:

// Java program to read a byte 
// from the user

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    byte byteVal = 0;

    Scanner SC = new Scanner(System.in);

    System.out.print("Enter byte value: ");
    byteVal = SC.nextByte();

    System.out.print("Byte value is: " + byteVal);
  }
}

Output:

Enter byte value: 15
Byte value is: 15

Example 2:

// Java program to read integer values 
// from the user

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    short shortVal = 0;
    int intVal = 0;
    long longVal = 0;

    Scanner SC = new Scanner(System.in);

    System.out.print("Enter 16-bit integer value: ");
    shortVal = SC.nextShort();

    System.out.print("Enter 32-bit integer value: ");
    intVal = SC.nextInt();

    System.out.print("Enter 64-bit integer value: ");
    longVal = SC.nextLong();

    System.out.println("The 16-bit integer value: " + shortVal);
    System.out.println("The 32-bit integer value: " + intVal);
    System.out.println("The 64-bit integer value: " + longVal);
  }
}

Output:

Enter 16-bit integer value: 15
Enter 32-bit integer value: 1234566
Enter 64-bit integer value: 1234567890
The 16-bit integer value: 15
The 32-bit integer value: 1234566
The 64-bit integer value: 1234567890

Example 3:

// Java program to read floating-point numbers 
// from the user

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    float floatVal = 0;
    double doubleVal = 0;

    Scanner SC = new Scanner(System.in);

    System.out.print("Enter 32-bit floating-point value: ");
    floatVal = SC.nextFloat();

    System.out.print("Enter 64-bit floating-point value: ");
    doubleVal = SC.nextDouble();

    System.out.println("The 32-bit floating-point value: " + floatVal);
    System.out.println("The 64-bit floating-point value: " + doubleVal);
  }
}

Output:

Enter 32-bit floating-point value: 12.34
Enter 64-bit floating-point value: 123456789.12345
The 32-bit floating-point value: 12.34
The 64-bit floating-point value: 1.2345678912345E8

Example 4:

// Java program to read a string 
// from the user

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    String strVal = "";

    Scanner SC = new Scanner(System.in);

    System.out.print("Enter String: ");
    strVal = SC.next();

    System.out.println("The string is: " + strVal);
  }
}

Output:

Enter String: Hello
The string is: Hello

Example 5:

// Java program to read a character 
// from the user

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    char charVal;

    Scanner SC = new Scanner(System.in);

    System.out.print("Enter character: ");
    charVal = SC.next().charAt(0);

    System.out.println("The character is: " + charVal);
  }
}

Output:

Enter character: C
The character is: C


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.