๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Scanner Class  


Introduction
  • The Scanner class in Java is used to take user input of primitive data types like int, double, etc., and strings.
  • It can read input from a user through the keyboard or from a file.
  • The Scanner class is present in the java.util package.
  • Syntax:
    public final class Scanner implements Iterator, Closeable
    {
        // methods
    }
    
Important Methods of Scanner Class:
  • Below are some common and mostly used methods of Scanner class with examples:
Method Description Example Output
nextInt() Reads the next token as an int.
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(n);
Depends on input (e.g., 10)
nextDouble() Reads the next token as a double.
Scanner sc = new Scanner(System.in);
double d = sc.nextDouble();
System.out.println(d);
Depends on input (e.g., 5.5)
nextLine() Reads the entire line including spaces until newline.
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.println(line);
Depends on input (e.g., Hello World)
next() Reads the next token (word) from input.
Scanner sc = new Scanner(System.in);
String word = sc.next();
System.out.println(word);
Depends on input (e.g., Hello)
hasNext() Checks if there is another token available.
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
    System.out.println(sc.next());
}
Prints all tokens one by one
hasNextInt() Checks if the next token can be interpreted as an int.
Scanner sc = new Scanner(System.in);
if(sc.hasNextInt()){
    int n = sc.nextInt();
    System.out.println(n);
}
Depends on input
useDelimiter(String pattern) Sets the delimiter pattern used to separate tokens.
Scanner sc = new Scanner("Java,Python,C++");
sc.useDelimiter(",");
while(sc.hasNext()){
    System.out.println(sc.next());
}
Java
Python
C++
close() Closes the scanner and releases resources.
Scanner sc = new Scanner(System.in);
sc.close();
Scanner is closed
Example:
  • import java.util.Scanner;
    
    public class ScannerExample
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
    
            System.out.print("Enter your name : ");
            String name = sc.nextLine();
    
            System.out.print("Enter your age : ");
            int age = sc.nextInt();
    
            System.out.print("Enter your salary : ");
            double salary = sc.nextDouble();
    
            System.out.println("--------------------------------");
    
            System.out.println("Name : " + name);
            System.out.println("Age : " + age);
            System.out.println("Salary : Rs. " + salary);
    
            sc.close();
        }
    }

Important Points:
  1. To create a Scanner object, we often pass System.in to read input from the keyboard. If we want to read from a file, we can pass a File object instead.
  2. To read numbers of a particular type, use the corresponding method nextXYZ(). For example:
    • To read int values, we use nextInt() method.
    • To read short values, we use nextShort() method.
  3. To read an entire line, we use nextLine() method.
    Example:
    
    import java.util.Scanner;
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a line:");
            String line = sc.nextLine();
            System.out.println("You entered: " + line);
            sc.close();
        }
    }
  4. To read a single character, we use next().charAt(0).
    Example (syntax):
    
    Scanner sc = new Scanner(System.in);
    char ch = sc.next().charAt(0);
            
  5. To read data from a file, we use Scanner(File file) constructor.
    Example:
    
    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;
    
    public class ReadFile
    {
        public static void main(String[] args) throws FileNotFoundException
        {
            File file = new File("input.txt");
            Scanner sc = new Scanner(file);
            while(sc.hasNextLine())
            {
                String line = sc.nextLine();
                System.out.println(line);
            }
            sc.close();
        }
    }
  6. Always close the Scanner object after use to avoid resource leaks.