How To Get The Standard Input From Console In Java

This simple example shows how to get the standard input from console in Java. When you run this program, it asks the user for the input, if you enter a value, it is printed in the console. Let’s try this example.

StdInputExample.java

package javabeat.net.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StdInputExample {
	public static void main(String args[]) {
		try {
			//BufferedReader with InputStreamReader
			BufferedReader bufferReader = new BufferedReader(new InputStreamReader(
					System.in));
			String inputValue;

			//Reads and prints it
			while ((inputValue = bufferReader.readLine()) != null) {
				System.out.println("You entered : "+inputValue);
			}
		} catch (IOException io) {
			io.printStackTrace();
		}
	}

}

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better