Skip to main content

Unit 4 · Data Collections

Text Files and Scanner

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What text files and scanner covers

Reading data from a text file is new to the 2026 AP CSA framework. A Scanner constructed over a File reads its input in order, and hasNext reports whether anything remains to read. The standard shape is a while loop that tests before every read: while (input.hasNext()), then a read method that matches the data, such as next, nextLine, nextInt, or nextDouble. Tokens are separated by whitespace, and runs of whitespace collapse into one separator, so leading spaces and blank runs never produce empty tokens.

Where students lose points

The most common error is a loop that reads without checking first, which throws once the input runs out or the file is empty. Test with hasNext before every read. Students also forget that using a File requires the program to say what happens if the file cannot be opened: adding throws IOException to the header of the method that uses the file is the form the course framework models, and both File and IOException come from java.io.

How it shows up on the AP exam

File and Scanner input was added to the course framework alongside data and its implications. Expect it in questions that read a set of records and compute a summary, which combines file input with the accumulator and array patterns you already know.

For practice use only.

Text Files and Scanner MCQ Practice

AP CSA text file and Scanner practice: hasNext loops, reading tokens and lines, throws IOException, and pulling records out of a data file.

Question 1 of 15

Answered 0 of 15

Choose one answer.

A method opens a file with Scanner and does not handle the failure itself. Which exception must its header declare?

Choose one answer.