Java program to read numbers from users

Java Programming Java8Java Technologies

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
raja
Updated on 13-Mar-2020 07:08:41

Advertisements