Learn how to read and print a string from user input in Java using the Scanner class with a simple, beginner-friendly example program.
import java.util.Scanner;
/**
* Simple program to read a string from input and print it.
*/
public class LetturaStringa {
public static void main(String[] args) {
// Creation of a Scanner object to read from console
Scanner scanner = new Scanner(System.in);
// Prompting the user to enter a string
System.out.print("Inserisci una stringa: ");
// Reading the line entered by the user
String inputUtente = scanner.nextLine();
// Printing the read string
System.out.println("Hai inserito: " + inputUtente);
// Closing the scanner to free resources
scanner.close();
}
}Java Development Kit (JDK) installed
Development environment or terminal to run Java programs
DEPENDENCIES: no external ones, uses only standard Java libraries
CONFIG: no configuration required