106k views
0 votes
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");

User Raad
by
6.7k points

1 Answer

5 votes

Answer:

Scanner in = new Scanner(hoursFile);

Step-by-step 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);

User Charmander
by
5.4k points