Simple example of how to use the Scanner class in Java to read input from the console
This simple code in Java, shows how to use the Scanner class to read data entered by the user in the console.
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello Java Programmer!");
System.out.println("What is your name?");
System.out.println();
String name = input.next();
System.out.println("Hello " + name + ", how old are you?");
System.out.println();
int age = input.nextInt();
if(age<18) {
System.out.println("Hello " + name + ", you are very young!");
} else if(age>80) {
System.out.println("Hello " + name + ", you are very old!");
}
System.out.println("Goodbye");
input.close();
}
}