To use the Scanner class to read input from the keyboard in Java, you can follow these steps:
nextLine()
method, which reads a line of text input from the keyboard:userInput
in your Java program as needed.import java.util.Scanner;
public class KeyboardInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter some text: ");
String userInput = scanner.nextLine();
System.out.println("You entered: " + userInput);
}
}
When you run this program, it will prompt the user to enter some text, and then it will print out the text that the user entered.