Steps for attaching scanner

  • What is Scanner?

    • A class that represents text-based parser.

    • It can parse text data from any source console input,text file, socket, string.

  import java.util.*;
  //OR 
  import java.util.Scanner;
  • Create instance of Scanner class

    • constructor: Scanner(InputStream in)

    • stdin: System.in

  Scanner sc=new Scanner(System.in);
  • To check data type of input type currenly present in the input strream use

    boolean hasNextInt();
    boolean hasNextByte();
    boolean hasNextLong();
    //and so on... 
  • To read data

  int nextInt() throws InputMismatchException
  double nextDouble() throws InputMismatchException
  String next();
  String nextLine();
  boolean nextBoolean();
  //and so on...

Last updated