The program below will ask an input from the user then it will display what the user entered.
import java.util.*;
public class java03{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scan.nextInt();
System.out.println("\nYour age is "+ age);
}
}
public class java03{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scan.nextInt();
System.out.println("\nYour age is "+ age);
}
}
output:
// the user must input integer here, example 20.
Enter your age: 20
// then it will display what the user entered.
Your age is 20
example:
- Scanner input1 = new Scanner(System.in);
- Scanner input2 = new Scanner(System.in);
import java.util.*; - This is the symbol for util package. If you will declare a command statement for Scanner, you must encode this symbol first at beginning of your code. Because this symbol holds the Scanner.