Reading User Input in Java


The Scanner class is used to get user input.

Source Code

Scanner myObj = new Scanner(System.in);
System.out.println("Enter username:");

String userName = myObj.nextLine(); // Read user input
System.out.println("Username is: " + userName);

Import java.util.Scanner and create a Scanner object to read user input from the console, enabling interactive Java applications.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments