Java - Reading User Input

In this code snippet we take a look at capturing user input using the Scanner class.

There are a few things to note about this code snippet. The first is the use of the import keyword, which is a java reserved keyword that is used to import existing classes into a program. Java like many programming languages comes with a library of existsing classes and these classes can be imported into your program.

The program below is importing the Scanner class from java.util. The Scanner class in this example is used to read user input from System.in.
  1. import java.util.Scanner;   
  2.   
  3. class Program {   
  4.   
  5.     public static void main(String args[]){   
  6.         System.out.println("Please enter your name");   
  7.         Scanner input = new Scanner(System.in);   
  8.         System.out.println("You entered " + input.nextLine());   
  9.     }   
  10. }  

No comments:

Post a Comment