How to read input from console – Java

We can read Java’s input from “System.in” console, there are two common ways to read input from console.

1) InputStreamReader wrapped in a BufferedReader
2) Scanner classes in JDK1.5

InputStreamReader , BufferedReader example

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class ReadConsoleSystem {
  public static void main(String[] args) {
 
	System.out.println("Enter something here : ");
 
	try{
	    BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
	    String s = bufferRead.readLine();
 
	    System.out.println(s);
	}
	catch(IOException e)
	{
		e.printStackTrace();
	}
 
  }
}

Scanner example

import java.util.Scanner;
 
public class ReadConsoleScanner {
  public static void main(String[] args) {
 
	  System.out.println("Enter something here : ");
 
	   String sWhatever;
 
       Scanner scanIn = new Scanner(System.in);
       sWhatever = scanIn.nextLine();
 
       scanIn.close();            
       System.out.println(sWhatever);
  }
}

BufferedReader or Scanner

Which one is better? Should we go for BufferedReader or Scanner? I will go for BufferedReader for one reason, i familiar with it :). Well Scanner is a new class since JDK1.5, it’s come out more easy way to read input from file, and code is more clean. I may use Scanner class in my future project ~

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Ashok

    Is it possible to getting console error in String..pls guide me or mail me…Advanced Thanks.

  • mayantha

    This was very helpful for my facial reconstruction simulation project…
    thank you..!!

  • Zulfiqar Ali

    Really nice tutorial…

  • http://vevits.com mark

    the buffered reader worked better than System.in thanks

    • Fikre

      You are so helpful. May God bless you.

      Can you give me a hand in using JDPAPI to protect some credentials like, password, username network address.

  • http://vevits.com mark

    reader worked well much better than System.in
    and worked…. thanks

  • Prakash

    Is it legal not to call close method of BufferedReader class while reading from console?

    • ANKUR

      Same question….

  • philip

    Since java 6 there is another shorter way (like in c#) to read/write from the system console :
    System.console().readLine();

    or just instatiate a Console reference (java.io.Console) and assign a it to the system’s console :

    Console c = System.console();
    c.readLine();

    • philip

      Also the Console way is thread safe the other two methods are not just to point out another difference. If you look in the source code of the java.io.Console class you will see that readLine uses locks to prevent concurrent read/write access.

      Personally i prefer the console way for simple and the scanner way, when you need to tokenize the input.

  • Mike

    Scanner IMHO is great, since it seems to me more concise, however if you enter dot’s and/or comma’s through numeric pad section of your keyboard, – be surprised to find out that you are not a citizen of Great Britain: Scanner mixes up numeric locales (I am from Russia just in case)

  • Angel

    Wow you are amazing, each day i randomly search in google and i find solutions in this page.
    thank you again!

  • vithalani

    great …… useful to me

  • Anand Raj

    I want to read spaces from input and the scanner throws an exception while doing so. I want to use every ‘other’ whitespace character as delimiter except space. Any help with scanner? Or shall I continue to use BufferedReader?
    I have very small inputs to fetch as in “Hello World” or “I am new”

  • http://wwes.com.br FĂ©rnas

    Useful for me, thanks!

  • Nanda

    But scanner will not be a good practice unless the input is very small. ( less than 50KB of data is to be read )

    • Nanda

      also can you provide which will be more efficient

  • Jarl

    This example is not a useful solution for me.
    I want to accept current/default number (by pressing enter) or change number by
    input number and press enter.
    Further if I happen to input a character java crashes, very bad !

    I haven’t found a solution yet,
    /Jarl

    • http://www.mkyong.com mkyong

      Can you provide steps to simulate how you crashed the Java console?

  • Umar

    What about the System.Io.Console class new to Java 6? It seems even simpler, but I heard it can produce glitches depending on whether console on the particular JVM is started automatically or not.

    • Umar

      I mean…java.io.console…

  • ScanMan

    I prefer Scanner personally. Great tutorial, thanks!

    • mkyong

      ya , Scanner provide many new functionality, may be i’m an old guy, used to BufferedReader already haha

  • Shah

    Excellent!! Very helpfull.