Scanner Example Programs

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 breaks the input into tokens using a delimiter which is white space by default. It provides many methods to read and parse various primitive values.

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.

The Java Scanner class extends Object class and implements Iterator and Closeable interfaces.
The Java Scanner class provides nextXXX() methods to return the type of value such as nextInt(), nextByte(), nextShort(), next(), nextLine(), nextDouble(), nextFloat(), nextBoolean(), etc. To get a single character from the scanner, you can call next().charAt(0) method which returns a single character.

Example Program :

import java.util.Scanner;  

public class ScannerExample {  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);           System.out.print("Enter your rollno: ");             int rollno= in.nextInt();  


           System.out.println("roll number is: " + rollno);             System.out.print("Enter your fee: ");             double fee= in.nextDouble();             System.out.println("fee amount is: " + fee);    

                     in.close();                       }  }  

Comments