How to Read Integer From Console in Java

104 20
    • 1). Open your Java Integrated Development Environment (IDE).

    • 2). Type the following into a Java file:

      System.out.println("Please enter an integer:");

    • 3). Type the following on the next line:

      Scanner in = new Scanner(System.in);

      Normally, if you simply wanted to read ordinary text data from the console, you would read it from "System.in" directly, but here you are using "System.in" to create a Scanner object that allows you to collect data from the console in any of a multitude of formats.

    • 4). Type the following on the next line to actually read the integer from the user:

      int i = in.nextInt();

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.