The readLine function in Kotlin is used to read a line of input from the standard input (usually the console). It is part of the Kotlin standard library and provides a simple way to read user input or input from a file.
Table of Contents
- Introduction
readLineFunction Syntax- Understanding
readLine - Examples
- Basic Usage
- Reading Multiple Lines
- Reading Input Until a Specific Condition
- Using
readLinewith Files
- Real-World Use Case
- Conclusion
Introduction
The readLine function allows you to read a single line of input from the standard input. This is useful for scenarios where you need to interact with the user, read configuration data, or process text input.
readLine Function Syntax
The syntax for the readLine function is straightforward:
fun readLine(): String?
Returns:
- A
String?(nullable string) representing the line of input read, ornullif the end of input has been reached.
Understanding readLine
readLine: Reads a single line of text from the standard input and returns it as a string. If the end of input is reached (e.g., end of file), it returnsnull.
Examples
Basic Usage
To demonstrate the basic usage of readLine, we will read a single line of input from the console.
Example
fun main() {
println("Please enter your name:")
val name = readLine()
println("Hello, $name!")
}
Output:
Please enter your name:
[User enters "Alice"]
Hello, Alice!
Reading Multiple Lines
This example shows how to read multiple lines of input from the console.
Example
fun main() {
println("Enter your favorite quotes (type 'end' to finish):")
val quotes = mutableListOf<String>()
while (true) {
val line = readLine() ?: break
if (line.lowercase() == "end") break
quotes.add(line)
}
println("Your favorite quotes:")
quotes.forEach { println(it) }
}
Output:
Enter your favorite quotes (type 'end' to finish):
[User enters "To be or not to be"]
[User enters "The only thing we have to fear is fear itself"]
[User enters "end"]
Your favorite quotes:
To be or not to be
The only thing we have to fear is fear itself
Reading Input Until a Specific Condition
This example shows how to read input until a specific condition is met.
Example
fun main() {
println("Enter numbers (type '0' to finish):")
val numbers = mutableListOf<Int>()
while (true) {
val line = readLine() ?: break
val number = line.toIntOrNull() ?: continue
if (number == 0) break
numbers.add(number)
}
println("You entered: $numbers")
}
Output:
Enter numbers (type '0' to finish):
[User enters "5"]
[User enters "10"]
[User enters "0"]
You entered: [5, 10]
Using readLine with Files
This example shows how to use readLine to read lines from a file using BufferedReader.
Example
import java.io.File
fun main() {
val file = File("example.txt")
file.writeText("Line 1\nLine 2\nLine 3")
val bufferedReader = file.bufferedReader()
bufferedReader.useLines { lines ->
lines.forEach { println(it) }
}
}
Output:
Line 1
Line 2
Line 3
Real-World Use Case
Processing User Input
In real-world applications, the readLine function can be used to process user input for various tasks, such as command-line interfaces, interactive prompts, or configuration settings.
Example
fun main() {
println("Enter your username:")
val username = readLine()
println("Enter your password:")
val password = readLine()
println("Username: $username")
println("Password: $password")
}
Output:
Enter your username:
[User enters "user123"]
Enter your password:
[User enters "secret"]
Username: user123
Password: secret
Conclusion
The readLine function in Kotlin provides a simple and effective way to read a line of input from the standard input or from files. By understanding and using the readLine function, you can efficiently manage and process text input in your Kotlin applications, ensuring that you can handle various input scenarios according to your requirements.