Readline Java

Share

Readline Java

In Java, you can use the readLine() method to read input from the user from the console. The readLine() method is part of the BufferedReader class, which is used to read text from a character-based input stream.

Here is a basic example of how to use the readLine() method to get input from the user:

java

import java.io.*;

public class Main {
public static void main(String[] args) throws IOException {
// Creates an InputStreamReader
InputStreamReader isr = new InputStreamReader(System.in);

// Creates a BufferedReader
BufferedReader br = new BufferedReader(isr);

System.out.println(“Enter text:”);

// Use the readLine method of BufferedReader class
String str = br.readLine();

 

System.out.println("You entered: " + str);
}
}

In this code:

  1. We first create an InputStreamReader object isr that takes System.in (which allows us to read data from the console) as the argument.
  2. We then create a BufferedReader object br that takes isr as the argument.
  3. We print a message prompting the user to enter some text.
  4. We call the readLine() method of br to read a line of text from the user.
  5. We print the text the user entered.

Remember to handle or declare the IOException that can be thrown by readLine(). In this example, the IOException is simply declared to be thrown by the main method, but in a more complex program, you would typically handle the exception in a more robust way.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *