How to Take Input From User in Java?

To take input from a user in Java, you can use the Scanner class which is a part of the Java standard library. Here's an example: 

 

import java.util.Scanner;

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

        System.out.print("Enter your name: ");
        String name = scanner.nextLine();

        System.out.print("Enter your age: ");
        int age = scanner.nextInt();

        System.out.println("Your name is " + name + " and you are " + age + " years old.");
    }
}

In this example, we create a new Scanner object and use it to read input from the user. We first ask the user to enter their name and store the input in a String variable called name. We then ask the user to enter their age and store the input in an int variable called age.

Finally, we print out a message that includes the values of name and age that were entered by the user.

Note that the Scanner class has many other methods for reading different types of input (e.g. nextBoolean(), nextDouble(), etc.) that you can use depending on the type of input you need to read.

Submit Your Programming Assignment Details