Readline Java
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:
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:
- We first create an
InputStreamReader
objectisr
that takesSystem.in
(which allows us to read data from the console) as the argument. - We then create a
BufferedReader
objectbr
that takesisr
as the argument. - We print a message prompting the user to enter some text.
- We call the
readLine()
method ofbr
to read a line of text from the user. - 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:
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