Java Read String from Console With Examples

Today we will look into different ways to read string from console in java.

Java Read String from Console

We can use below classes to read input from console.

  1. BufferedReader
  2. Scanner

Java Read String from Console using BufferedReader

BufferedReader class is from Java IO package. We can use BufferedReader to read text from a character-input stream. Below is a simple program to read string data from console and print it in lower case.

Below is the output produced by one sample run of above program.

Java Read from Console using Scanner

If you notice above code, BufferedReader is not flexible and has very limited options. We can only read input data as String. That’s why Scanner class was introduced in Java 1.5 to read from console with many options.

We can use Scanner class to read as String, primitive data types such as int, long etc. We can also read input numbers in different bases such as binary and read it to integer. Let’s have a look at how to use Scanner class to read input data from console.

Below is the output produced by above program when executed and input is provided in Console.

That’s all for java read from console program or how to read string from console in java.

By admin

Leave a Reply