Reading Input from Console in Java


Scanner is a great class for getting console input from the user.

Source Code

import java.util.Scanner; // Import the Scanner class
class MyClass {
    public static void main(String[] args) {
        Scanner myObj = new Scanner(System.in);  // Create a Scanner object
        System.out.println("Enter username");
        String userName = myObj.nextLine();  // Read user input
        System.out.println("Username is: " + userName);  // Output user input
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments