Java

Console readline() Java

“This article will discuss the console Read line function in java. This function is used in java programs to gather input for the program from the user through the console interface. This function can be called within a variable to save the input in that same variable and store it for later use, or it can be used separately to ask the flow of the program. The console read line function also allows the format argument to be passed in it as a parameter. We will be implementing several examples of the read line function in different circumstances to get a better understanding of this function.”

Example # 01: Using the Console Read Line Function to Save the Input in a String Variable in Ubuntu 20.04

In this example, we will use the console read line function in our java program to input a name through the console and save it in the string variable, which will then be printed on the terminal after the program ends.

In the above program, we started by creating a public class in which we will create a main function. We will create a Console object and pass it to the system in the main procedure. console value that lets systems do tasks. Then we will initialize a string variable, and in its value, we will use the console object to call the read line function. In this function, we will also write a prompt message for gathering input. And then, the input will be saved in the string variable, which we will print on the terminal by passing it into the system.out.println function.

After saving the program, we will open out the terminal and execute it. In the terminal, we will get the prompt message to enter a name, and after entering the name, we will see the name displayed in the next line, which verifies that the name was saved in the string variable as seen in the above snippet.

Example # 02: Using the Console Read Line Function to Save the Input in a String Variable With a Format Parameter in Ubuntu 20.04

We will use the console read line method in our java program to enter a name into the console and save it in the string variable along with a format argument, which will then be printed on the terminal when the program has finished; in this example.

In the preceding code, we began by defining a public class that will house the main function. In the main method, we will build a Console object and provide it to the system. Console value that allows systems to do actions. We will establish a string variable named “fmt” to hold the java format in which a string can be saved, and this will be supplied as an input to the read line method. Then, in the value of another string variable, we will utilize the console object to run the read line method. This method will use the format string variable we defined before and a prompt message to gather input. The input will then be saved in a string variable, which will be printed on the terminal by feeding it into the system.println function output.

After we save the program, we will open the terminal and run it. In the terminal, we will get a prompt message to input a name, and after entering the name, the name will be displayed in the following line, confirming that the name was stored in the string variable as seen in the previous sample.

Example # 03: Using the Console Read Line Function to Save the Input in a String Variable With a Format Parameter in Ubuntu 20.04

In this example, we will use the read line function and the buffered reader class object to determine the flow of the program by the state of the user’s input.

In this piece of code, we will be staring inside the main function, where we will create an object of the Buffered reader class. Then we’ll set up a string variable to save the input. Then we will add two alert messages where we will guide the user to determine the length of the program. After this, we will add a “do while” loop so that the input can be gathered repeatedly. Inside the do block, we will call the string variable, and in its value, we will use the object of the buffered reader class to call the read line function.

Then we will pass the string variable in the println function to display it on the terminal. After this, we will move into the while block, and in its parentheses, we will give the decision statement, which will analyze the user’s input and end the program when the user inputs the word “stop”.

After terminating all the remaining parentheses, we will move on to our terminal, compiling and executing our program. When the program is executed, the alert messages will be displayed as guidance. Then the user can input multiple words in the terminal, and the program will continue until the word stop is written, as shown in the output below.

Example # 04: Using the Read Line Function to Enter a Word in a Text File in Ubuntu 20.04

In this example, we will add a string in a text file using the read line function and the objects of the buffered writer class and the reader class.

In the above code, we have created two objects of the buffered writer class and the reader class, respectively. Then we initialized a string variable using the read line function to get the input from the user. After this, we started a while loop in which we stated that the loop would continue until the user gave null input. Inside the loop, there is another read line function called; after that, the input will be displayed on the terminal.

As we can see in the output above, the program terminates when we provide null input, and in the snippet below, the input is successfully added to the text file as well.

Conclusion

In this guide, we explore the console read line function in java. This function is used for gathering input from the user and can be used to transfer the input to be saved in a variable at run time. We implemented several examples of this function in different scenarios. We used it to save the input in a string variable and even insert the input value into a text file in the Ubuntu 20.04 environment.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.