What is priming read java?

1.5K    Asked by JoanGill in Java , Asked on Oct 6, 2022

I was taught this expression and pattern way back in the day. Sure, the name comes from old pumps that needed to be filled with water before they could pump water, but who cares? We're talking about code here.

Some really good examples and an explanation of what the pattern accomplishes would be welcome. How is this pattern regarded today?

Priming can sometimes get a defective loop working but at the cost of DRY. So it may be a brief stop on the way to a better design. Is this considered an anti-pattern? Are there alternatives?


Answered by Joanne Glover

This metaphor almost certainly refers to the practice of establishing the first conditional check in a while loop. If you don't do this, the loop won't work. It is a well-established pattern, and it hasn't changed since the while loop was invented. The requirement for setting the initial condition in a while loop is not a defect.


int i = 0; // prime the pump
while (i < 10>
{
    Console.Write("While statement ");
    Console.WriteLine(i);
    i++; // set condition again
}

The primer can be a read statement, or whatever properly sets the initial condition. Setting the initial condition using a read statement is called a "Priming Read java."

string line;

using (StreamReader file = new StreamReader("c:\test.txt"))
{
    line = file.ReadLine(); // Priming read.
    while(line != null)
    {
        Console.WriteLine (line);
        line = file.ReadLine(); // Subsequent reads.
    }
}In C#, the two Readline() calls can be combined into a single statement within the conditional:
while ((line = r.ReadLine()) != null)
{
    Console.WriteLine (line);
}

Your Answer

Answer (1)

In the context of Java, the term "priming read" often appears in discussions related to input processing, particularly when reading data from a stream or user input. A priming read is a technique used to initialize a loop that processes input. Here’s a detailed explanation of what a priming read is and how it is used:

What is a Priming Read?

A priming read is an initial read operation performed before entering a loop that processes input data. The main purpose of the priming read is to set up the loop condition properly. This technique is commonly used in scenarios where you need to process a stream of input until a certain condition is met (e.g., end of file, a specific sentinel value).

Why Use a Priming Read?

The primary reason for using a priming read is to avoid redundant or extra read operations within the loop body. It ensures that the condition for continuing the loop is checked before attempting to process any data within the loop.

Example of Priming Read in Java

Consider a scenario where you need to read lines from the console until the user enters a blank line:

1.Without Priming Read:

      import java.util.Scanner;public class WithoutPrimingRead {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String input;        while (true) {            System.out.print("Enter a line (or blank line to quit): ");            input = scanner.nextLine();            if (input.isEmpty()) {                break;            }            System.out.println("You entered: " + input);        }    }}

In this example, the read operation is performed twice within the loop: once to get the input and once to check the condition.

2. With Priming Read:

      import java.util.Scanner;public class WithPrimingRead {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String input;        // Priming read        System.out.print("Enter a line (or blank line to quit): ");        input = scanner.nextLine();        while (!input.isEmpty()) {            System.out.println("You entered: " + input);            // Subsequent read inside the loop            System.out.print("Enter a line (or blank line to quit): ");            input = scanner.nextLine();        }    }}

In this example, the priming read initializes the input before entering the loop. The loop condition is checked right after the priming read, and subsequent reads are performed inside the loop. This avoids redundancy and makes the loop condition clearer.

Benefits of Priming Read

  1. Clear Loop Initialization: The loop is properly initialized with the first read operation, making the logic clearer.
  2. Avoid Redundant Reads: Reduces the need to duplicate read operations, which can simplify the code.
  3. Handles Edge Cases: Ensures that the input condition is checked before processing any data, which can be important for handling edge cases (e.g., empty input).

Summary

A priming read in Java is an initial read operation performed before entering a loop that processes input. It helps in setting up the loop condition properly and avoids redundant read operations within the loop. This technique is useful for scenarios where you need to process a stream of input data until a certain condition is met.








6 Months

Interviews

Parent Categories