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.

Example

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

Output

Enter a number ::
5564
Number entered is :: 5564

Updated on: 13-Mar-2020

477 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements