Skip to main content

How to Read Input From Console in Java

If you have to read input from console in Java there are 3 options.

  1. Using BufferedReader class. See example.
  2. Using Scanner class, available from Java 5. See example.
  3. Using System.console() method, available from Java 6. See example.

Using BufferedReader class

By wrapping the standard input stream "System.in" in an InputStreamReader which is further wrapped with in a BufferedReader you can read input from console in Java.

public class ConsoleReader {
  public static void main(String[] args) {
    // Wrapping InputStreamReader and System.in
    BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter City Name: ");            
    try {            
      String cityName = bufferRead.readLine();
      System.out.println("Entered city name- " + cityName);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
Output
Enter City Name: Delhi
Entered city name- Delhi

Using Scanner class

Another option to read input from console in Java is to use Scanner class. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values. For scanning input there are convenient methods for different data types like nextInt(), nextDouble(), nextFloat() etc.

public class ConsoleReader {
  public static void main(String[] args) {
    System.out.print("Enter Values: ");                             
    Scanner sc = new Scanner(System.in);
    System.out.println("Entered Line- " + sc.nextLine());
    System.out.println("Entered integer value- " + sc.nextInt());
    System.out.println("Entered double value- " + sc.nextInt());
    sc.close();
  }
}
Output
Enter Values: Delhi
Entered Line- Delhi
56 67.89
Entered integer value- 56
Entered double value- 67.89

Using System.console method

You can also use System.console to read input from console in Java. One of the advantage of using System.console() method is that it returns a Console object which has a method readPassword() which can be used for reading a password or passphrase from the console with echoing disabled.

public class ConsoleReader {
  public static void main(String[] args) {
    //Using System.console()
    String username = System.console().readLine("Enter City Name: ");   
    System.out.println("Entered city name- " + username); 
  }
}
Output
Enter City Name: Delhi
Entered city name- Delhi
Using System.console method to read password
public class ConsoleReader {
  public static void main(String[] args) {
    //Using System.console()
    System.out.print("Enter password: ");
    char[] username = System.console().readPassword();   
    System.out.println("Entered password- " + username.toString()); 
  }
}
Output
Enter password:
Entered password- [C@75bd9247

As you can see echoing for the password is disabled.

That's all for the topic How to Read Input From Console in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like