Skip to main content

User Input and Output

Trushi Jasani
EditReport

User Input and Output

Output: Printing to Consoleโ€‹

println() โ€” Print with Newlineโ€‹

println("Hello, World!")
println(42)
println(3.14)
println(true)
print("Hello, ")
print("World!")
// Output: Hello, World!

System.out.printf() โ€” Formatted Outputโ€‹

System.out.printf("Name: %s, Age: %d, Score: %.2f%n", "Alice", 25, 98.5)
// Output: Name: Alice, Age: 25, Score: 98.50

String Templatesโ€‹

val name = "Bob"
val score = 95
println("Student: $name scored $score marks")
println("Next year score: ${score + 5}")

Input: Reading from Consoleโ€‹

readLine() โ€” Read a Line of Textโ€‹

fun main() {
print("Enter your name: ")
val name = readLine()
println("Hello, $name!")
}

Note: readLine() returns String? (nullable String).

Reading and Converting Inputโ€‹

fun main() {
print("Enter your age: ")
val age = readLine()?.toInt() ?: 0
println("You are $age years old")
}

Reading Different Data Typesโ€‹

Integer Inputโ€‹

fun main() {
print("Enter a number: ")
val num = readLine()!!.toInt()
println("Square: ${num * num}")
}

Double Inputโ€‹

fun main() {
print("Enter price: ")
val price = readLine()!!.toDouble()
println("With tax: ${price * 1.18}")
}

Boolean Inputโ€‹

fun main() {
print("Are you a student? (true/false): ")
val isStudent = readLine()!!.toBoolean()
println("Student status: $isStudent")
}

Safe Input Handlingโ€‹

Always handle potential errors when reading user input:

fun main() {
print("Enter a number: ")
val input = readLine()
val num = input?.toIntOrNull()

if (num != null) {
println("Double: ${num * 2}")
} else {
println("Invalid number entered!")
}
}

Multiple Inputsโ€‹

fun main() {
print("Enter first number: ")
val a = readLine()!!.toInt()

print("Enter second number: ")
val b = readLine()!!.toInt()

println("Sum: ${a + b}")
println("Difference: ${a - b}")
println("Product: ${a * b}")
}

Formatted Output with trimIndentโ€‹

fun main() {
val name = "Alice"
val age = 22
val gpa = 3.85

val report = """
====== Student Report ======
Name : $name
Age : $age
GPA : ${"%.2f".format(gpa)}
============================
""".trimIndent()

println(report)
}

Output:

====== Student Report ======
Name : Alice
Age : 22
GPA : 3.85
============================

Using Scanner (Java Interop)โ€‹

import java.util.Scanner

fun main() {
val scanner = Scanner(System.`in`)

print("Enter your name: ")
val name = scanner.nextLine()

print("Enter your age: ")
val age = scanner.nextInt()

println("Hello $name, you are $age years old!")
scanner.close()
}

Common Conversion Functionsโ€‹

FunctionConverts ToSafe Version
toInt()InttoIntOrNull()
toDouble()DoubletoDoubleOrNull()
toLong()LongtoLongOrNull()
toFloat()FloattoFloatOrNull()
toBoolean()BooleanN/A

Summaryโ€‹

  • Use println() for output with newline, print() without.
  • Use readLine() to read console input โ€” it returns String?.
  • Convert input using .toInt(), .toDouble(), etc.
  • Use safe variants like .toIntOrNull() to avoid crashes.
  • String templates with $ and ${} make output formatting easy.
Telemetry Integration

Completed working through this block? Sync progress to workspace.