Wednesday, September 7, 2022

How to Read Input From Console in Java

In this post three ways are given to read user input from keyboard (console) in Java-

  • First way uses the InputStreamReader wrapped in a BufferedReader.
  • Second way to read input from console uses the Scanner class from Java 5.
  • Third way uses the System.console which was introduced in Java 6.

Read input from console in Java using BufferedReader

public class ReadFromConsole {

 public static void main(String[] args) {
  // Using BufferedReader
  System.out.print("Please enter user name : ");   
  BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
  String s;
  try {   
   s = bufferRead.readLine();
   System.out.println("You entered- " + s);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }    
 }
}

Output

Please enter user name : netjs
You entered- netjs

In the program you can see that an InputStreamReader is wrapped with in a BufferedReader to read text from a character-input stream.

InputStreamReader wraps System.in where in is a field in a System class. in is the "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

Read input from console using Scanner

Scanner class, added in Java 5 is another option to read input from console in Java. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. nextLine() method of the Scanner class advances this scanner past the current line and returns the input that was skipped.

Scanner class also has methods for different data types like nextInt(), nextDouble(), nextFloat() etc.

public class ReadFromConsole {
  public static void main(String[] args) {
    // Using Scanner 
    System.out.print("Please enter user name : ");
    Scanner scanIn = new Scanner(System.in);
    String scanLine = scanIn.nextLine();
    System.out.println("You entered- " + scanLine);
    System.out.println("Entered int value- " + scanIn.nextInt());
    System.out.println("Entered float value- " + scanIn.nextFloat());
    scanIn.close();      
  }
}

Output

Please enter user name : nets
You entered- nets
3 78.90
Entered int value- 3
Entered float value- 78.9

Read input from console using System.console

console method in System class returns the unique Console object associated with the current Java virtual machine, if any.

A word of caution, if running the code from eclipse, System.console() will throw null pointer exception.

Please follow this discussion to know more about this exception- http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide
public class ReadFromConsole {
  public static void main(String[] args) {
    //Using System.console()
    String username = System.console().readLine("Please enter user name : ");   
    System.out.println("You entered : " + username);      
  }
}

Output

Please enter user name : netjs
You entered : netjs

That's all for this topic How to Read Input From Console in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Read File From The Last Line in Java
  2. How to Find Last Modified Date of a File in Java
  3. How to Read Properties File in Java
  4. Read or List All Files in a Folder in Java
  5. Zipping Files And Folders in Java

You may also like-

  1. Count Number of Words in a String Java Program
  2. Check if Given String or Number is a Palindrome Java Program
  3. How to Find Common Elements Between Two Arrays Java Program
  4. Producer-Consumer Java Program Using ArrayBlockingQueue
  5. Difference Between Abstract Class And Interface in Java
  6. Fail-Fast Vs Fail-Safe Iterator in Java
  7. Difference Between throw And throws in Java
  8. Volatile Keyword in Java With Examples