Reading Runtime Values from Keyboard and Properties File

Java 10 min min read Updated: Mar 31, 2026 Beginner
Reading Runtime Values from Keyboard and Properties File
Beginner Topic 8 of 25

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.

Key Concept: Runtime values are values provided when the program is running. In Java, they can be read interactively from the keyboard or loaded from external files such as .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:

java int age = 25;

In the above example, the value 25 is fixed inside the code.

Example of runtime value:

java System.out.print("Enter age: "); int age = scanner.nextInt();

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:

  • Scanner class
  • BufferedReader class
  • Console class

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

java import java.util.Scanner;

Example: Reading an Integer

java import java.util.Scanner; public class InputDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter age: "); int age = sc.nextInt(); System.out.println("Your age is: " + age); } }

Explanation

  • Scanner sc = new Scanner(System.in); creates a Scanner object connected to keyboard input
  • nextInt() reads an integer value
  • The value is stored in the variable age

Example: Reading a String

java import java.util.Scanner; public class InputDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.next(); System.out.println("Hello, " + name); } }

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().

java import java.util.Scanner; public class InputDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your full name: "); String fullName = sc.nextLine(); System.out.println("Welcome, " + fullName); } }

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

java import java.util.Scanner; public class StudentInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter name: "); String name = sc.nextLine(); System.out.print("Enter age: "); int age = sc.nextInt(); System.out.print("Enter marks: "); double marks = sc.nextDouble(); System.out.println("Student Name: " + name); System.out.println("Age: " + age); System.out.println("Marks: " + marks); } }

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

java import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter age: "); int age = sc.nextInt(); System.out.print("Enter full name: "); String name = sc.nextLine(); System.out.println("Age: " + age); System.out.println("Name: " + name); } }

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.

java import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter age: "); int age = sc.nextInt(); sc.nextLine(); // consume leftover newline System.out.print("Enter full name: "); String name = sc.nextLine(); System.out.println("Age: " + age); System.out.println("Name: " + name); } }
Important: After using 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

java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class InputDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter your name: "); String name = br.readLine(); System.out.println("Hello, " + name); } }

Explanation

  • InputStreamReader(System.in) converts byte stream into character stream
  • BufferedReader reads the input efficiently
  • readLine() reads a full line of input

Example: Reading Integer with BufferedReader

Since readLine() returns a String, it must be converted into an integer manually.

java import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class InputDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter age: "); int age = Integer.parseInt(br.readLine()); System.out.println("Age is: " + age); } }

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:

java import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = System.console(); if (console != null) { String name = console.readLine("Enter name: "); System.out.println("Hello, " + name); } else { System.out.println("Console not available"); } } }

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:

text .properties

These files are commonly used to store application settings outside the code.

Example:

text username=admin password=12345 url=jdbc:mysql://localhost:3306/testdb port=8080

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

text username=admin password=secret123 url=jdbc:mysql://localhost:3306/schooldb port=8080

Java Program to Read Properties File

java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class PropertyDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("config.properties"); Properties prop = new Properties(); prop.load(fis); String username = prop.getProperty("username"); String password = prop.getProperty("password"); String url = prop.getProperty("url"); String port = prop.getProperty("port"); System.out.println("Username: " + username); System.out.println("Password: " + password); System.out.println("URL: " + url); System.out.println("Port: " + port); } }

Explanation

  • FileInputStream reads the file
  • Properties prop = new Properties(); creates Properties object
  • prop.load(fis); loads key-value pairs into memory
  • getProperty("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.

java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class PropertyDemo { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("config.properties"); Properties prop = new Properties(); prop.load(fis); int port = Integer.parseInt(prop.getProperty("port")); System.out.println("Port number: " + port); } }

Using Default Value with getProperty()

Java allows a default value if the key does not exist.

java String host = prop.getProperty("host", "localhost"); System.out.println(host);

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

text db.url=jdbc:mysql://localhost:3306/mydb db.username=root db.password=pass123

Java Code

java import java.io.FileInputStream; import java.util.Properties; public class DBConfigReader { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.load(new FileInputStream("db.properties")); String url = prop.getProperty("db.url"); String username = prop.getProperty("db.username"); String password = prop.getProperty("db.password"); System.out.println(url); System.out.println(username); System.out.println(password); } }

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

java import java.util.Scanner; import java.util.Properties; import java.io.FileInputStream; public class AppConfigDemo { public static void main(String[] args) throws Exception { Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); Scanner sc = new Scanner(System.in); System.out.print("Enter your name: "); String name = sc.nextLine(); String appName = prop.getProperty("app.name", "MyJavaApp"); System.out.println("Welcome " + name); System.out.println("Application Name: " + appName); } }

Common Errors While Reading Keyboard Input

1. InputMismatchException

This happens when the input type does not match the expected type.

java Scanner sc = new Scanner(System.in); int age = sc.nextInt();

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 Practice: Always validate user input and handle exceptions while reading data from keyboard or configuration files.

Best Practices

  • Use Scanner for beginner-friendly keyboard input
  • Use BufferedReader when line-based input or performance matters
  • Do not hardcode passwords and configuration in source code
  • Store reusable settings in .properties files
  • Always close resources properly or use try-with-resources
  • Validate and parse runtime input carefully

Interview-Oriented Points

  • Scanner is the most common class for keyboard input in Java
  • BufferedReader reads text and may require parsing for numeric values
  • Properties class is used to read key-value pairs from a properties file
  • next() reads one word, while nextLine() 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.

Quick Summary: Java can read runtime values from the keyboard using classes like Scanner and BufferedReader, and it can load external configuration values from properties files using the Properties class.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators