Write Java program to how to accept Userinput from keyboard


The Java program demonstrates how to take user input using the Scanner class. The program creates an object of the Scanner class and takes the user input from the keyboard using the nextLine() method of the Scanner class. It prompts the user to enter their name and then stores the input in a string variable called str. Finally, the program prints the user input using the println() method of the System.out object along with the text "Name : ".

Source Code

import java.util.Scanner;  
class UserInput
{    
	public static void main(String args[])
	{ 
		// Scanner is a class  
		Scanner input = new Scanner(System.in);    
		System.out.print("Enter the Name : "); 
 
		/*nextLine() method of scanner class 
		which takes userinput from keyboard */   
 
		String str = input.nextLine();    
		System.out.println("Name : " +str);
	}  
}

Output

Enter the Name : Tutor Joes
Name : Tutor Joes