Consider the following code snippet. File hoursFile = new File("hoursWorked.txt"); Your program must read the contents of this file using a Scanner object. Which of the following is the correct syntax for doing this? Scanner in = new Scanner("hoursWorked.txt"); Scanner in = new Scanner(hoursFile); Scanner in = Scanner.open(hoursFile); Scanner in = Scanner("hoursWorked.txt");

Respuesta :

Answer:

Scanner in = new Scanner(hoursFile);

Explanation:

The syntax to read the content of a file after the file has been created/initialized is as follows;

Scanner [variable-name] = new Scanner([object-name]);

From the question above, the [object-name] is hoursFile

Substitute [object-name] with hoursFile, the syntax becomes

Scanner [variable-name] = new Scanner(hoursFile);

[variable-name] can be anything as long as it follows variable naming convention;

From the list of given options, the option that answers the question is: Scanner in = new Scanner(hoursFile);