All TalkersCode Topics

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

Reading Console Input In Java

Last Updated : Mar 11, 2024

Reading Console Input In Java

In this article we will show you the solution of reading console input in java, here we are using eclipse tools so itself its importing necessary package during each program creation.

To achieve the goal of output you need to use console() and readLine() methods with system statement. The returning input stored on a variable and then displayed to the terminal.

Step By Step Guide On Reading Console Input In Java :-

Before using console() and readLine() methods you need to know about them actual process, right? Let’s see.

The console() class is be used to get input from console and provides methods to read texts and passwords.

The readLine() method is used to read single line of text from console. It returns null if the stream has ended.

In main class ‘RdCsl_prgm’ we provide static main method definition. There first you need to disclose some message regards enter input. Then you have to collect user input through console().readLine() statements in java.

The returned output stored on string variable and at last displayed output with some string.

//Reading Console Input Example
package java_prgms;
class RdCsl_prgm
 public static void main(String args[])
  System.out.println("Enter Console Input");
  String txt=System.console().readLine();
  System.out.println("Result is "+txt);
  1. The ‘Package java_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. In main class ‘RdCsl_prgm’ we provide public static void main method definition. Within that method we displayed message ‘Enter Console Input’ to clarify users about to enter input through standard output method of println().
  3. To collect console input first you need to run the program on console in proper way. Then there you can see the above seen text, now end user needs to enter input as per their choice.
  4. Which is retrieved by ‘System.console().readLine();’ this line and storing returned result to string variable ‘txt’.
  5. Again using println() method we just printed result of string output with we defined ‘Result is’.
  6. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  7. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion, now we are able to know how to read console input in java. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java RdCsl_prgm‘ line for gets the output.

Those are the running statements might do it on system terminal with proper location path will leads you to get input and output.

Otherwise, you cannot get output, for instance you gave string ‘Welcome’ then it show you message 'Result is Welcome'.

I hope this article on reading console input in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪