How to Accept Input From the Keyboard in Java?

You are currently viewing How to Accept Input From the Keyboard in Java?

How to accept input from the keyboard in java?

import java.util.Scanner;  

class ScannerClass{    
	public static void main(String args[]){ 
		/* Scanner is a class of java.util package */  
		Scanner sc = new Scanner(System.in);    
		System.out.println("Enter Hobbies "); 

		/*next() is a method of scanner class 
		which takes input from keyboard */   
		
		String hobbies = sc.next();    
		System.out.println("Your Hobbies are " +hobbies);    
	}  
}
import java.io.Console;  

class ConsoleClass{    
	public static void main(String args[]){
		/* Console is a class of java.util package */  

		Console con = System.console();    
		System.out.println("Enter programming skills "); 

		/*readLine() is a method of Console class 
		which takes input from keyboard */   

		String skills = con.readLine();    
		System.out.println("Your skills are "+skills);    
	}    
}
import java.io.*;  

class InputStreamReaderClass{  
	public static void main(String args[])throws Exception{  
		InputStreamReader isr = new InputStreamReader(System.in);  
		BufferedReader br = new BufferedReader(isr);  		  
		System.out.println("Enter programming skills");  
		String skills = br.readLine();  
		System.out.println(" Your Skills are "+skills);  
	}  
}

Output

D:\Java Articles>java ScannerClass
Enter Hobbies
Hockey,Volleyball
Your Hobbies are Hockey,Volleyball
D:\Java Articles>java ConsoleClass
Enter programming skills
c,c++,java,python
Your skills are c,c++,java,python  
D:\Java Articles>java InputStreamReaderClass
Enter programming skills
c,c++,java,android
Your Skills are c,c++,java,android

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply