In Java, you can get user input using the Scanner class. Here's a simple example:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
// Create a Scanner object
Scanner scanner = new Scanner(System.in);
// Prompt the user for input
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Read user input
// Display the input
System.out.println("Hello, " + name + "!");
// Close the scanner
scanner.close();
}
}
In this code:
Scanner scanner = new Scanner(System.in);creates a newScannerobject to read input from the console.scanner.nextLine();reads a line of text entered by the user and stores it in the variablename.- Finally, the program greets the user with their name.
Feel free to ask if you have more questions about Java or user input!
