Make output print once per loop, not each iteration

Joshua Napier

I have a class that is designed to search a file for a user-specified string. The class works as it should, cycling through a file containing multiple lines and printing - provided the specified string exists - the line(s) containing the desired string. The issue I'm having at the moment is that my else statement (provide new word if previous word does not exist) runs for every line (as it should), but I only want it to run once per loop. Here is my code

public class SimpleDBSearch {

  public void sdbSearch(Scanner searchWord) throws IOException{

    //Prompt user for input
    System.out.println("Please input the word you wish to find:");

    //Init string var containing user input
    String wordInput = searchWord.next();

    //Specify file to search
    File file = new File("C:/Users/Joshua/Desktop/jOutFiles/TestFile.txt");

    //Init Scanner containing specified file
    Scanner fileScanner = new Scanner(file);

    //Loops through every line looking for lines containing previously specified string.
    while(fileScanner.hasNextLine()){
        String line = fileScanner.nextLine();
        if(line.contains(wordInput)){       //If desired string is found, print line containing it to console
            System.out.println("I found the word you're looking for here: " + line);
        }else{      //If desired string not found, prompt user for new string. I want this to occur only once, not per every line-check
            System.out.println("Please input a new word");
        }
    }
}
Tim B

Set a boolean flag to false before entering the loop.

If you find a line set it to true.

Once the loop has finished check the flag and write the message as appropriate.

boolean found=false;

Inside loop

found = true;

Outside loop

if (!found) {
    // do stuff
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using CompletableFuture in a Loop with Two Futures to Merge per Loop Iteration

From Dev

Ensuring an observer fires once per run loop

From Dev

Can I print immediately for each iteration in a loop?

From Dev

Print loop output horizontally?

From Dev

add results of a for loop after each iteration

From Dev

await task in each iteration of a loop

From Dev

Add delay on each loop iteration

From Dev

Loop 'n' elements in groups in each iteration

From Dev

How to write on different output files at each iteration of loop? (Perl)

From Dev

jQuery skip an iteration of the parent loop in nested .each

From Dev

How can I make speech occur immediately for each iteration of a while loop in Swift?

From Dev

Refactoring for loop with each iteration setting a different property

From Dev

Delay each loop iteration in node js, async

From Dev

Loop through an array and print out a template for each iteration

From Dev

Is it better to initialize a loop iterator once per scope?

From Dev

Storing Each Iteration of Array after Loop Completion

From Dev

Segregating results per iteration of a loop

From Dev

How can I change this code to make the progress bars appear for each file and the iteration be each loop?

From Dev

Print loop output horizontally?

From Dev

Emptying lists per loop iteration in python?

From Dev

Loop iteration and output result in single line.

From Dev

Delaying For Loop Between Each Iteration

From Dev

Running a loop precisely once per second

From Dev

Iterate print for each line in output

From Dev

xslt: Loop only once in a for-each loop

From Dev

How to make a loop that uses 3 of indexes each iteration without specifying last two iterations in turbo-pascal?

From Dev

How to a make a loop in java that will print a slightly different string on each line?

From Dev

How to store loop output of each iteration to data frame

From Dev

Print once after for loop

Related Related

  1. 1

    Using CompletableFuture in a Loop with Two Futures to Merge per Loop Iteration

  2. 2

    Ensuring an observer fires once per run loop

  3. 3

    Can I print immediately for each iteration in a loop?

  4. 4

    Print loop output horizontally?

  5. 5

    add results of a for loop after each iteration

  6. 6

    await task in each iteration of a loop

  7. 7

    Add delay on each loop iteration

  8. 8

    Loop 'n' elements in groups in each iteration

  9. 9

    How to write on different output files at each iteration of loop? (Perl)

  10. 10

    jQuery skip an iteration of the parent loop in nested .each

  11. 11

    How can I make speech occur immediately for each iteration of a while loop in Swift?

  12. 12

    Refactoring for loop with each iteration setting a different property

  13. 13

    Delay each loop iteration in node js, async

  14. 14

    Loop through an array and print out a template for each iteration

  15. 15

    Is it better to initialize a loop iterator once per scope?

  16. 16

    Storing Each Iteration of Array after Loop Completion

  17. 17

    Segregating results per iteration of a loop

  18. 18

    How can I change this code to make the progress bars appear for each file and the iteration be each loop?

  19. 19

    Print loop output horizontally?

  20. 20

    Emptying lists per loop iteration in python?

  21. 21

    Loop iteration and output result in single line.

  22. 22

    Delaying For Loop Between Each Iteration

  23. 23

    Running a loop precisely once per second

  24. 24

    Iterate print for each line in output

  25. 25

    xslt: Loop only once in a for-each loop

  26. 26

    How to make a loop that uses 3 of indexes each iteration without specifying last two iterations in turbo-pascal?

  27. 27

    How to a make a loop in java that will print a slightly different string on each line?

  28. 28

    How to store loop output of each iteration to data frame

  29. 29

    Print once after for loop

HotTag

Archive