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();
		}
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>