initialise variable at beginning and move scanner.close in finally block