Reading Runtime Values from Keyboard and Properties File
In real Java applications, values are not always hardcoded inside the source code. Many times, a program needs to accept input while it is running, or it needs to load configuration values from an external file. That is why learning how to read runtime values is an important part of Java programming.
Runtime values can come from different sources, but two very common sources are:
- Keyboard input entered by the user
- Configuration values stored in a properties file
Keyboard input is useful when a user is actively interacting with the program, while properties files are commonly used for application settings such as database URL, username, password, API keys, and environment-based configuration.
.properties files.
What are Runtime Values?
Runtime values are values that are supplied to a program while it is executing, rather than values fixed in the source code. They make the program flexible and dynamic.
Example of hardcoded value:
In the above example, the value 25 is fixed inside the code.
Example of runtime value:
Here, the value is provided by the user while the program is running.
Why Runtime Input is Important
Runtime input is important because real applications cannot depend only on fixed values. They need to accept user input and configuration dynamically.
- ATM software asks for amount and PIN at runtime
- Student form asks for name, age, and marks
- Database application reads username and password from file
- Server applications load port numbers and URLs from config files
Reading Input from Keyboard in Java
Java provides multiple ways to read runtime values from the keyboard. The most common approaches are:
ScannerclassBufferedReaderclassConsoleclass
Among these, Scanner is the most beginner-friendly and commonly used for learning Java input.
1. Reading Input Using Scanner Class
The Scanner class belongs to the java.util package. It is used to read different types of input such as integers, strings, floats, doubles, and booleans from the keyboard.
Import Statement
Example: Reading an Integer
Explanation
Scanner sc = new Scanner(System.in);creates a Scanner object connected to keyboard inputnextInt()reads an integer value- The value is stored in the variable
age
Example: Reading a String
next() reads only one word up to the first space.
Example: Reading a Full Line
To read a complete sentence or full line including spaces, use nextLine().
Different Scanner Methods
The Scanner class provides multiple methods for different data types:
| Method | Purpose |
|---|---|
nextInt() |
Reads integer |
nextDouble() |
Reads double value |
nextFloat() |
Reads float value |
nextBoolean() |
Reads boolean value |
next() |
Reads one word |
nextLine() |
Reads a full line |
Example: Reading Multiple Values with Scanner
Common Scanner Problem: nextLine() After nextInt()
One of the most common beginner mistakes happens when nextLine() is used after nextInt() or similar methods. The leftover newline character remains in the input buffer, so the next nextLine() may read an empty line.
Problem Example
In many cases, name becomes empty because the newline from nextInt() is consumed by nextLine().
Correct Solution
Consume the leftover newline before reading the next full line.
nextInt(), nextDouble(), or similar Scanner methods, call nextLine() once before taking a full line input.
2. Reading Input Using BufferedReader
Another traditional way to read input in Java is by using BufferedReader. It belongs to the java.io package and is often considered faster than Scanner for some use cases.
It reads input as text, so explicit conversion may be needed for numeric values.
Example: Reading String with BufferedReader
Explanation
InputStreamReader(System.in)converts byte stream into character streamBufferedReaderreads the input efficientlyreadLine()reads a full line of input
Example: Reading Integer with BufferedReader
Since readLine() returns a String, it must be converted into an integer manually.
Scanner vs BufferedReader
| Feature | Scanner | BufferedReader |
|---|---|---|
| Ease of use | Easy | Moderate |
| Reads primitive types directly | Yes | No |
| Needs parsing for numbers | No | Yes |
| Suitable for beginners | Yes | Less suitable |
| Performance | Good | Often faster |
3. Reading Input Using Console Class
Java also provides the Console class through System.console(). It is useful in some command-line environments and supports secure password input.
Example:
However, this method may not work in all IDEs, so beginners mostly use Scanner.
What is a Properties File?
A properties file is a text file used to store key-value pairs. It usually has the extension:
These files are commonly used to store application settings outside the code.
Example:
This approach is useful because configuration can be changed without editing Java source code.
Why Use Properties Files?
- keeps configuration separate from source code
- easy to update without recompiling code
- helps in environment-based setup
- useful for database, server, and API configurations
- improves maintainability
Reading Data from a Properties File
Java provides the Properties class from the java.util package to read key-value pairs from a properties file.
Sample File: config.properties
Java Program to Read Properties File
Explanation
FileInputStreamreads the fileProperties prop = new Properties();creates Properties objectprop.load(fis);loads key-value pairs into memorygetProperty("key")reads the value of a specific key
Reading Numeric Values from Properties File
All values in a properties file are read as strings. If you need a number, convert it manually.
Using Default Value with getProperty()
Java allows a default value if the key does not exist.
If host is not present in the properties file, localhost will be used.
Example: Database Configuration Reading
This is a very practical use case of properties files.
db.properties
Java Code
In real applications, these values are later used to create database connections.
Combining Keyboard Input and Properties File
Sometimes both sources are used together. For example, an application may load default configuration from a properties file and ask the user to enter some runtime values manually.
Example
Common Errors While Reading Keyboard Input
1. InputMismatchException
This happens when the input type does not match the expected type.
If the user enters text instead of an integer, Java throws InputMismatchException.
2. nextLine() Issue After Numeric Input
As discussed earlier, a newline remains in the buffer after methods like nextInt().
3. Forgetting to Import Scanner or IO Classes
Missing imports cause compile-time errors.
Common Errors While Reading Properties File
1. FileNotFoundException
If the properties file does not exist at the specified path, Java throws FileNotFoundException.
2. IOException
This may occur during reading or loading the file.
3. Null Values
If a key does not exist, getProperty() may return null.
4. NumberFormatException
If a non-numeric string is converted into an integer using Integer.parseInt(), Java throws NumberFormatException.
Best Practices
- Use
Scannerfor beginner-friendly keyboard input - Use
BufferedReaderwhen line-based input or performance matters - Do not hardcode passwords and configuration in source code
- Store reusable settings in
.propertiesfiles - Always close resources properly or use try-with-resources
- Validate and parse runtime input carefully
Interview-Oriented Points
Scanneris the most common class for keyboard input in JavaBufferedReaderreads text and may require parsing for numeric valuesPropertiesclass is used to read key-value pairs from a properties filenext()reads one word, whilenextLine()reads a full line- Properties files are useful for external configuration management
- All property values are read as strings and must be converted if numeric values are needed
Conclusion
Reading runtime values makes Java programs dynamic and practical. Keyboard input allows user interaction, while properties files allow externalized configuration without changing source code.
A strong Java programmer should know both approaches because they are used in real-world applications regularly. From simple console-based programs to enterprise applications, runtime value handling is a core skill in Java development.

