Inputs

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Input your name: ");
        String name = scanner.nextLine();

        System.out.println("Input your age: ");
        int age = scanner.nextInt();

        System.out.println("Hello "  + name + ", you are "+ age + " years old.");

    }
}

The snippet result is

Input your name:

momo

Input your age: 18

Hello momo, you are 18 years old.

You can use System.in to input something from console. You could use nextLine() to get value from the new line. You could use nextInt to get integer value from console. You could use hasNextInt to define whether the input is integer or not. return boolean

Last updated