What does a call to scanner()
do? It looks like you're calling it twice each time you loop, and may be discarding the first call (but checking it) and storing the 2nd call and not checking it. Methinks you should call it only once for each loop.
Note, your code has other problems:
- There's no need for the
scanner()
method. - There's no need to keep re-creating a Scanner object, and in fact by doing so and not releasing resources, you run the risk of running out of resources.
- Instead create your Scanner object once, and use it inside of your try block, calling
nextLine()
on it as needed. - Also consider closing your file *after the while loop, and in fact in the finally block of your try block. Close your Scanner object there as well.
- It's often a better idea to check your String with
equalsIgnoreCase(...)
. - It's safer to check it like so:
String line = null;
while ("exit".equalsIgnoreCase(line)) {
line = scanner.nextLine();
// etc...
To avoid NPE