Skip to content
Advertisement

how to properly check if a string is in txt file

I’m writing a program that manages a library. This part of the program is where you borrow a book, but only if it’s in the library, so I check if the book is in the library but it always comes back negative even if the string is in the library txt file.

JavaScript

What should I do?

Advertisement

Answer

You are just checking the first line. If you want to check if the book exists anywhere in the file, you need to continue reading it until you find the book or until you reach the end of the file:

JavaScript

Note that using Files.lines could easier than using a reader:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement