Java program to read numbers from users

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.

  • 1. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
  • 2. A scanning operation may block waiting for input.
  • 3. A Scanner is not safe for multi-threaded use without external synchronization.

The nextInt() method of the Scanner class is used to read an integer value from the source.


import java.util.Scanner;
public class ReadingNumbersFromUser {
   public static void main(String args[]){
      Scanner sc = new Scanner(;
      System.out.println("Enter a number ::");
      int num = sc.nextInt();
      System.out.println("Number entered is :: "+num);


Enter a number ::
Number entered is :: 5564