Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Scanner class in AP Computer Science A

In AP Computer Science A, the Scanner class is a built-in Java class that reads text input. It's introduced in Topic 1.4 as one way to get keyboard input, and in Topic 4.6 it's paired with a File object to read data stored in a text file.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is the Scanner class?

The Scanner class is a built-in Java class that reads text input and hands it to your program piece by piece. Think of it as a reader that sits on top of an input source and pulls out the next number, word, or line whenever you ask. The CED introduces it in Topic 1.4 (EK 1.4.B.1) as "one way to obtain text input from the keyboard."

Scanner gets its real AP workout in Topic 4.6. A file is storage that persists after your program stops running (EK 4.6.A.1). To read one, you connect it to your program with the File and Scanner classes (EK 4.6.A.2). First you create a File object using the file name as the constructor argument, like new File("studentData.txt") (EK 4.6.A.3). Then you pass that File object to a Scanner. Because Scanner is a reference type, you create it with new, store it in a variable, and call methods on it. Common examples are nextInt(), nextLine(), hasNext(), and close().

Why the Scanner class matters in AP® Computer Science A

Scanner shows up in two units. In Unit 1: Using Objects and Methods (Topic 1.4), it supports AP Comp Sci A 1.4.B, "Develop code to read input." It's also one of your first real examples of creating an object from a class and calling its methods. In Unit 4: Data Collections (Topic 4.6), it supports AP Comp Sci A 4.6.A, "Develop code to read data from a text file." Here Scanner is how data gets into your arrays and ArrayLists in the first place. One CED detail to know: the exclusion statement in 1.4.B says any specific form of user input is outside the scope of the exam. So the heavier exam emphasis is on file reading in 4.6, not on typing at a keyboard.

How the Scanner class connects across the course

File class (Unit 4)

File and Scanner are a team. File says which file to open, and Scanner actually reads what's inside it. You need both, because a File object on its own can't pull out any data. The CED also requires your code to say what happens if the file can't be found, which is part of setting this connection up correctly.

Reference Types and Initialization (Unit 1)

A Scanner variable works like any other object variable. It holds a reference, it has to be initialized (usually with new Scanner(...)) before you use it, and it's null if no object has been assigned yet. Learning Scanner in Topic 1.4 is practice for the object rules you'll use all year.

nextInt() and Data Types (Unit 1)

Scanner's methods are typed. nextInt() gives you an int, nextDouble() gives you a double, and nextLine() gives you a String. EK 1.4.A.1 says a stored value must come from a compatible data type, so you need to match the method to the variable on the left side of the =.

Reading file data into collections (Unit 4)

In Unit 4, Scanner is the front door for data. A typical pattern loops while hasNext() is true and adds each value to an array or ArrayList. Once the file is closed, you process the data with the traversal algorithms from the rest of the unit.

Is the Scanner class on the AP® Computer Science A exam?

Multiple-choice questions on Scanner tend to test recognition and method choice, not long code. Here are the stem types you'll see:

  • Which class reads text input? Example: "A programmer needs to let a user enter their age from the keyboard. Which Java class should be used?" The answer is Scanner.
  • Which method fits the data? Example: reading a line of text that may contain spaces calls for nextLine(), not next(). next() stops at the first space.
  • File vs. Scanner roles. Example: "Which class creates an object that represents 'studentData.txt'?" The answer is File, not Scanner. This is a favorite trap.
  • What counts as text input? You may need to pick out a text-input example from options that include other forms of input.

No released FRQ has used the term verbatim. Still, file-reading code can set up data that an FRQ then asks you to process with arrays or ArrayLists. Be able to read a short Scanner loop and say what ends up stored in each variable.

The Scanner class vs File class

A File object represents the file. You build it with the file name, as in new File("data.txt"), but it doesn't read anything. A Scanner object reads the contents, and you build it by passing in that File, as in new Scanner(myFile). If a question asks which class "represents" or "opens" a named file, pick File. If it asks which class reads the values, pick Scanner.

Key things to remember about the Scanner class

  • The Scanner class is a built-in Java class that reads text input, either from the keyboard (Topic 1.4) or from a text file (Topic 4.6).

  • To read a file, you create a File object with the file name and then pass that File object into the Scanner constructor.

  • Scanner is a reference type, so you create it with new and call methods like nextInt(), nextLine(), hasNext(), and close() on it.

  • Use nextLine() when the input may contain spaces, because next() only reads up to the next whitespace.

  • The value a Scanner method returns has to match the variable's data type, so nextInt() goes into an int and nextLine() goes into a String.

  • Specific forms of user input are excluded from the AP exam, so file reading in Topic 4.6 is where Scanner matters most.

Frequently asked questions about the Scanner class

What is the Scanner class in AP Computer Science A?

It's a built-in Java class that reads text input and returns it as values your program can store, such as ints, doubles, and Strings. The CED covers it in Topic 1.4 for keyboard input and in Topic 4.6 for reading text files alongside the File class.

Is keyboard input with Scanner tested on the AP CSA exam?

Not in any detailed way. The CED's exclusion statement for 1.4.B says any specific form of user input is outside the scope of the exam. You should still recognize Scanner as the class for text input, and you should know how it reads data from files under 4.6.A.

What's the difference between the Scanner class and the File class?

File represents the file itself, and you create it with the file name, like new File("studentData.txt"). Scanner reads the data inside, and you create it by passing that File object to its constructor. You need both to read a file.

Should I use next() or nextLine() to read text with spaces?

Use nextLine(). It reads the whole rest of the line, spaces included, and returns it as a String. next() stops at the first whitespace, so it would only grab the first word.

Do I need an import statement to use Scanner?

Yes. Scanner isn't available automatically, so real Java code imports it with import java.util.Scanner;. File is imported with import java.io.File; when you read from a file.