Read int by using Scanner Class : Scanner « Development Class « Java






Read int by using Scanner Class

  


import java.util.Scanner;

public class MainClass {

  static Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    System.out.print("Enter an integer: ");
    int x = sc.nextInt();
    System.out.println("You entered " + x + ".");
  }

}

   
  








Related examples in the same category

1.Use Scanner to read user input
2.Count all vowels inputed from keyboard
3.Use Scanner to compute an average of the values
4.Use Scanner to compute an average of the values in a file
5.Use Scanner to read various types of data from a file
6.Use Scanner to compute an average a list of comma-separated values
7.Demonstrate findInLine()
8.use Scanner to read input that contains several different types of data
9.Letting the user decide when to quit
10.Test the input string in the while condition.
11.This program demonstrates console input.
12.Reading double value from console with ScannerReading double value from console with Scanner