All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Take String Input From User In Java

Last Updated : Mar 11, 2024

How To Take String Input From User In Java

In this article we will show you the solution of how to take string input from user in java, it speaks of the procedure for allowing user input in a form of a string of character into a Java programme.

In order to do this, one of the many Java methods, like the Scanner class and the BufferedReader class, must be used to read user data from the standard input stream as well as additional input sources.

The approach chosen will rely on the particular needs of the programme, such as the input format or the kind of processing needed.

Now let's talk about the idea of taking user-provided string input in Java.

Step By Step Guide On How To Take String Input From User In Java :-

public class Test {
 public static void main(String[] args)
  throws IOException
  BufferedReader reader = new BufferedReader(
   new InputStreamReader(;
  String name = reader.readLine();
  1. As you can see, we have written a simple Java programme that read a string from the console & displays it to the console.
  2. Import is used in line one of the code.The import statement BufferedReader; in Java enables the programme to apply the BufferedReader class that is included in the package.
  3. Classes for Java input as well as output operations are available in the package.
  4. This package contains the BufferedReader class, which is used for reading text from a character input stream.
  5. After that, import is used.Java's import statement for the IOException class obtained from the package enables the programme to use it. It is written as IOException;.
  6. This package contains the IOException class, which is used to manage input/output errors that could happen while working with files or streams.
  7. Next, import is used.Java's InputStreamReader class obtained from the package can be used by the programme by using the import statement inputstreamreader;.
  8. This package contains the InputStreamReader class, which is used to read bytes from an InputStream and convert them into characters using a given character encoding.
  9. A new class named Test was then declared.
  10. Afterwards, we define the Test class's main method, which serves as the program's entry point.
  11. The method can be accessible from anywhere and doesn't need to be generated as an object of the Test class thanks to the "public static" keyword.
  12. The term "void" denotes that the method returns nothing.
  13. When the programme is run, the main method may receive an array of strings via the "String[] args" parameter.
  14. A "throws IOException" clause denotes that if an input/output issue occurs, the main procedure might throw an IOException.
  15. Then, using an InputStreamReader object that receives input from standard input stream (, we construct a new BufferedReader object called "reader" that reads input from the console.
  16. The byte stream is changed into a character stream by the InputStreamReader so that a BufferedReader can read it.
  17. Then, using the BufferedReader object previously constructed, it reads a line of text from the console & assigns the result to new String variable called "name."
  18. The System.out.println method is then used to print the value of the variable "name" to the console.

Conclusion :-

As a result, we have successfully learnt the Java idea of accepting user-provided string input.

We also discovered that this Java programme uses the built-in input/output streams & the Java IO library to read a string input at the console window and output it to the console.

I hope this article on how to take string input from user in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪