1.04 Assignment Statements and Input
Assign and update variables, tell assignment apart from equality, and read input with Scanner.
- Reference Guide
- Real-Life Application
- Tech Talk
- Code Examples
- Code Runner Challenge
- Code Runner Challenge
- Hacks & Practice Tasks
- Code Runner Challenge
- MCQ Check
- Homework Hack
- Code Runner Challenge
- Code Runner Challenge
- Code Runner Challenge
Reference Guide
| Term | Definition / Example | College Board Requirement |
|---|---|---|
| Assignment statement | Stores the value of the right side in the variable on the left: x = x + 5; |
Unit 1, Topic 1.4, 1.4.A.2, p. 35 |
| Assignment operator | = means “store,” not “is equal to.”: score = 90; |
Unit 1, Topic 1.4, 1.4.A.2, p. 35 |
| Equality operator | == compares two values and gives true or false: score == 90 |
— |
| Expression | Code that works out to a single value with a type: (7 + 8) / 2.0 |
Unit 1, Topic 1.4, 1.4.A.3, p. 35 |
| Input | Data that comes from outside the program: Keyboard, a web request | Unit 1, Topic 1.4, 1.4.B.1, p. 35 |
| Scanner | A class that reads text input: Scanner sc = new Scanner(System.in); |
Unit 1, Topic 1.4, 1.4.B.1, p. 35. Specific input code is outside the exam scope. |
- A variable needs a value before it can be used on the right side
- Changing a variable later does not change a copy made from it earlier
Assignment Step by Step
| Code | What Java does | Values after this line |
|---|---|---|
int a = 5; |
Stores 5 | a is 5 |
a = a * 2; |
Works out 5 * 2 first, then stores it |
a is 10 |
int b = a; |
Copies the current value of a |
a is 10, b is 10 |
a = 1; |
Replaces a; b keeps its copy |
a is 1, b is 10 |
Setting Up Scanner
| Step | Code |
|---|---|
| 1. Import the class | import java.util.Scanner; |
| 2. Make one Scanner | Scanner sc = new Scanner(System.in); |
| 3. Prompt, then read | System.out.print("Age: "); then int age = sc.nextInt(); |
| 4. Close it when done | sc.close(); |
Scanner Methods
The Scanner class provides several methods for reading different types of input:
nextInt()- Reads the next integer value (e.g.,42)nextDouble()- Reads the next decimal number (e.g.,3.14)next()- Reads the next word up to whitespace (e.g.,"Hello")nextLine()- Reads an entire line including spaces (e.g.,"Hello World")
Picking a Scanner Method
- Whole number →
nextInt() - Decimal →
nextDouble() - One word →
next() - A full line with spaces →
nextLine()
The nextInt() and nextLine() Problem
nextInt(), nextDouble(), and next() read their value but leave the Enter key behind. The next nextLine() grabs that leftover and returns an empty String right away.
Oracle’s docs explain why: nextLine() “returns the rest of the current line, excluding any line separator at the end” (Oracle, n.d.). After nextInt(), the rest of the line is empty.
The fix: add one extra sc.nextLine(); after nextInt() or nextDouble().
College Board Requirements
AP CSA Unit 1, Topic 1.4 Assignment Statements and Input. Quoted from the course and exam description (College Board, 2025, p. 35):
- 1.4.A.2 “The assignment operator = allows a program to initialize or change the value stored in a variable. The value of the expression on the right is stored in the variable on the left.”
- 1.4.A.3 “During execution, an expression is evaluated to produce a single value. The value of an expression has a type based on the evaluation of the expression.”
- 1.4.B.1 “Input can come in a variety of forms, such as tactile, audio, visual, or text. The Scanner class is one way to obtain text input from the keyboard.”
Note for the exam: specific input code is not tested. The College Board says “any specific form of input from the user is outside the scope of the AP Computer Science A course and exam” (College Board, 2025, p. 35). You still need Scanner for real programs.
Reference: College Board, AP Computer Science A Course and Exam Description (2025), Unit 1, Topic 1.4, p. 35.
Real-Life Application
One Missing Equals Sign That Almost Took Over Linux
In 2003, someone slipped two lines into the Linux kernel’s source code, the code that runs servers, Android phones, and much of the internet. It looked like a harmless safety check. Everyone find the one character that turns this check into a takeover before you read the table:
if ((options == (__WCLONE|__WALL)) && (current->uid = 0))
retval = -EINVAL;
| What It Looks Like | What It Really Does | Java Idea in This Lesson |
|---|---|---|
current->uid == 0 would ask “is this user the administrator?” |
current->uid = 0 makes the user the administrator (user 0 is root) |
== compares; = stores a value |
| A check that quietly fails | The attacker gets full control, and the program keeps running normally | An assignment works out the right side, then stores it on the left |
| Caught before release | Reviewers spotted the change before it shipped | Read every = as “store” and every == as “is equal to” |
Input is the other half of this lesson, and the College Board lists four forms of it. Every app on your phone uses at least one:
| Input Form | Real Example | Java Version in This Lesson |
|---|---|---|
| Tactile | Tapping a touchscreen | Not covered |
| Audio | Talking to a voice assistant | Not covered |
| Visual | Scanning a QR code with the camera | Not covered |
| Text | Typing your name into a form | Scanner: nextLine(), nextInt(), nextDouble() |
The takeaway: one character can change what a program does. Trace every = and every input value line by line.
Tech Talk
Learning Objective: Write assignment statements with expressions, say what value each variable holds, and read input with Scanner.
Success Criteria: You can trace the value in a variable after each line, set up a Scanner, read int, double, and String input, and fix the leftover newline after nextInt().
Assignment works right to left. Java finishes the whole right side first, then stores the result on the left.
int x = 10;
x = 20; // 10 is gone
x = x + 5; // right side is 20 + 5, so x is 25
String name = null; // null means it points at no object
The type is decided before the value is stored, which surprises people:
double average = (7 + 8) / 2; // 7.0, because (7 + 8) / 2 is int math
double better = (7 + 8) / 2.0; // 7.5
= stores a value. == compares two values. Input is anything that comes from outside the program. Scanner is one way to read text from a keyboard.
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
The code runner has no keyboard, so every example in this lesson reads its answers from a String, such as new Scanner("7\n8\n10\n"), instead of new Scanner(System.in). In your own IDE, switch back to System.in to type real answers.
Code Examples
A. Reading input. The InputDemoRunner class reads a String, an int, and a double with Scanner
The runner has no keyboard, so this copy reads the answers from a String. Change them and run it again.
Common reading mistake:
int age = sc.nextInt(); // reads 16 but leaves the Enter key behind
String name = sc.nextLine(); // grabs the leftover Enter, so name is ""
Terminology:
Scanner is a reference type: new Scanner(...) makes one object, and each call like nextLine(), nextInt(), or nextDouble() reads the next value from it.
Run it with the simulated answers, then change them
import java.util.Scanner;
public class InputDemoRunner {
public static void main(String[] args) {
Scanner sc = new Scanner("Alex Rivera\n16\n2.5\n"); // simulated keyboard input
// Reading a String (entire line)
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
// Reading an integer
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("You entered: " + age);
// Reading a double
System.out.print("Enter a decimal: ");
double value = sc.nextDouble();
System.out.println("Double that is: " + (value * 2));
sc.close(); // Always close to prevent resource leaks
}
}
Code Runner Challenge
Run it with the simulated answers, then change them
View IPYNB Source
// CODE_RUNNER: Run it with the simulated answers, then change them
import java.util.Scanner;
public class InputDemoRunner {
public static void main(String[] args) {
Scanner sc = new Scanner("Alex Rivera\n16\n2.5\n"); // simulated keyboard input
// Reading a String (entire line)
System.out.print("Enter your name: ");
String name = sc.nextLine();
System.out.println("Hello, " + name + "!");
// Reading an integer
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.println("You entered: " + age);
// Reading a double
System.out.print("Enter a decimal: ");
double value = sc.nextDouble();
System.out.println("Double that is: " + (value * 2));
sc.close(); // Always close to prevent resource leaks
}
}
InputDemoRunner.main(null);
B. Full input program. The InputLessonRunner class reads three answers and prints a summary
These are the Popcorn Hack inputs. Trace the output first, then run it.
Common assignment mistake:
age + 1; // works out 21 but stores it nowhere, so Java rejects it: not a statement
age = age + 1; // right side first, then store: age is now 21
Terminology:
Assignment: Java works out the whole right side first, then stores the result in the variable on the left. (age + 1) in the summary only prints next year’s age; age itself stays 20.
Trace the output first, then run it to check
import java.util.Scanner;
public class InputLessonRunner {
public static void main(String[] args) {
System.out.println("=== User Information Program ===\n");
Scanner sc = new Scanner("Alice Wonderland\n20\n3.85\n"); // simulated keyboard input
// String input
System.out.print("Enter your full name: ");
String name = sc.nextLine();
// Integer input
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Double input
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble();
// Display results
System.out.println("\n--- Summary ---");
System.out.println("Name: " + name);
System.out.println("Age: " + age + " (next year: " + (age + 1) + ")");
System.out.println("GPA: " + gpa);
sc.close();
}
}
Code Runner Challenge
Trace the output first, then run it to check
View IPYNB Source
// CODE_RUNNER: Trace the output first, then run it to check
import java.util.Scanner;
public class InputLessonRunner {
public static void main(String[] args) {
System.out.println("=== User Information Program ===\n");
Scanner sc = new Scanner("Alice Wonderland\n20\n3.85\n"); // simulated keyboard input
// String input
System.out.print("Enter your full name: ");
String name = sc.nextLine();
// Integer input
System.out.print("Enter your age: ");
int age = sc.nextInt();
// Double input
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble();
// Display results
System.out.println("\n--- Summary ---");
System.out.println("Name: " + name);
System.out.println("Age: " + age + " (next year: " + (age + 1) + ")");
System.out.println("GPA: " + gpa);
sc.close();
}
}
InputLessonRunner.main(null);
Hacks & Practice Tasks
Lesson: CSA 1.4 Assignment Statements and Input
MCQ 1.4: <paste the copied result line, such as 3/4 | answers: C,B,C,B>
Popcorn: predicted Summary = Name <n>, Age <a> (next year <b>), GPA <g>; actual = Name <n>, Age <a> (next year <b>), GPA <g>
Homework Hack 1: all three checks print true (yes/no); = stores, == compares, bug explained = <one line>
Homework Hack 2: input check prints true (yes/no)
Homework Hack 3: average = <value> with two decimals, both checks print true (yes/no)
Popcorn Hack
2-minute challenge: trace Example B before you run it, then make it about you.
- Read the answers in the
Scanner: “Alice Wonderland”, 20, and 3.85 - Write the exact Summary you expect in the comment, before you run
- Run it and compare the output with your trace
- Replace the answers with your own name, age, and GPA
- Trace again, then run to check
- Answer the question in the Popcorn checker under the runner before you run. When every check passes, copy its line into your notes
Write what each variable holds after every line.
Write your trace in the comment first, then run it
// Practice #1 - Try it yourself first!
import java.util.Scanner;
public class MyInfoTrace {
public static void main(String[] args) {
// My trace (write it before you run):
// --- Summary ---
// Name:
// Age:
// GPA:
Scanner sc = new Scanner("Alice Wonderland\n20\n3.85\n"); // simulated keyboard input
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble();
System.out.println("\n--- Summary ---");
System.out.println("Name: " + name);
System.out.println("Age: " + age + " (next year: " + (age + 1) + ")");
System.out.println("GPA: " + gpa);
sc.close();
}
}
Code Runner Challenge
Write your trace in the comment first, then run it
View IPYNB Source
// CODE_RUNNER: Write your trace in the comment first, then run it
// Practice #1 - Try it yourself first!
import java.util.Scanner;
public class MyInfoTrace {
public static void main(String[] args) {
// My trace (write it before you run):
// --- Summary ---
// Name:
// Age:
// GPA:
Scanner sc = new Scanner("Alice Wonderland\n20\n3.85\n"); // simulated keyboard input
System.out.print("Enter your full name: ");
String name = sc.nextLine();
System.out.print("Enter your age: ");
int age = sc.nextInt();
System.out.print("Enter your GPA: ");
double gpa = sc.nextDouble();
System.out.println("\n--- Summary ---");
System.out.println("Name: " + name);
System.out.println("Age: " + age + " (next year: " + (age + 1) + ")");
System.out.println("GPA: " + gpa);
sc.close();
}
}
MyInfoTrace.main(null);
Write your trace here before you press Run, for the answers in your Scanner.
MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your notes.
Homework Hack
Task: Do all three hacks: assignment first, then input, then both together. Each starter compiles before you edit it, so you can run the initial version and compare its incorrect output with the expected output. Change only what the TODO asks for, and keep going until every check prints true.
Optional: Game. Play the 1.3 and 1.4 Game, screenshot your score, and send it to the 1.3 form (it covers 1.3 and 1.4). It is extra practice and is not graded.
Solution Skeleton:
Hack 1: = versus ==
Assignment statements. Explain the difference between = and ==, show a code example of each, then explain a bug that happens when they get mixed up. The starter below is your code example: use = to store 90 in score and == to check it. Put both explanations in your notes.
Use = and == so all three checks print true.
public class AssignmentHack {
public static void main(String[] args) {
int score = 80;
// TODO: use = to store 90 in score.
// TODO: replace false with a == comparison that checks whether score is 90.
boolean isNinety = false;
int copy = score;
score = 0; // copy keeps the value it was given
System.out.println("score: " + score + ", copy: " + copy);
System.out.println(isNinety);
System.out.println(copy == 90);
System.out.println(score == 0);
}
}
Code Runner Challenge
Use = and == so all three checks print true.
View IPYNB Source
// CODE_RUNNER: Use = and == so all three checks print true.
public class AssignmentHack {
public static void main(String[] args) {
int score = 80;
// TODO: use = to store 90 in score.
// TODO: replace false with a == comparison that checks whether score is 90.
boolean isNinety = false;
int copy = score;
score = 0; // copy keeps the value it was given
System.out.println("score: " + score + ", copy: " + copy);
System.out.println(isNinety);
System.out.println(copy == 90);
System.out.println(score == 0);
}
}
AssignmentHack.main(null);
Hack 2: Read Three Numbers
Input. Read three integers with Scanner. Replace each 0 with the method from Picking a Scanner Method that reads a whole number.
Read the three numbers with Scanner so the check prints true.
import java.util.Scanner;
public class ReadInputHack {
public static void main(String[] args) {
// The runner has no keyboard, so the three numbers come from a String.
Scanner sc = new Scanner("7\n8\n10\n");
System.out.print("Enter three whole numbers: ");
// TODO: replace each 0 with a call that reads the next whole number.
int first = 0;
int second = 0;
int third = 0;
System.out.println(first + ", " + second + ", " + third);
System.out.println(first + second + third == 25);
sc.close();
}
}
Code Runner Challenge
Read the three numbers with Scanner so the check prints true.
View IPYNB Source
// CODE_RUNNER: Read the three numbers with Scanner so the check prints true.
import java.util.Scanner;
public class ReadInputHack {
public static void main(String[] args) {
// The runner has no keyboard, so the three numbers come from a String.
Scanner sc = new Scanner("7\n8\n10\n");
System.out.print("Enter three whole numbers: ");
// TODO: replace each 0 with a call that reads the next whole number.
int first = 0;
int second = 0;
int third = 0;
System.out.println(first + ", " + second + ", " + third);
System.out.println(first + second + third == 25);
sc.close();
}
}
ReadInputHack.main(null);
Hack 3: Three-number Average
Combined. Read three integers, average them, and print the result with two decimals. Watch for integer division. Skeleton below.
Fill in the blanks so both checks print true.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
// The runner has no keyboard, so the three numbers come from a String.
Scanner sc = new Scanner("7\n8\n10\n");
System.out.print("Enter three whole numbers: ");
int first = sc.nextInt();
int second = sc.nextInt();
int third = sc.nextInt();
// 1. Add them, then divide WITHOUT losing the decimals
double average = 0.0;
// 2. Print the average with two decimal places
System.out.println("Average: " + average);
System.out.println(average > 8.33);
System.out.println(average < 8.34);
}
}
Code Runner Challenge
Fill in the blanks so both checks print true.
View IPYNB Source
// CODE_RUNNER: Fill in the blanks so both checks print true.
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
// The runner has no keyboard, so the three numbers come from a String.
Scanner sc = new Scanner("7\n8\n10\n");
System.out.print("Enter three whole numbers: ");
int first = sc.nextInt();
int second = sc.nextInt();
int third = sc.nextInt();
// 1. Add them, then divide WITHOUT losing the decimals
double average = 0.0;
// 2. Print the average with two decimal places
System.out.println("Average: " + average);
System.out.println(average > 8.33);
System.out.println(average < 8.34);
}
}
AverageCalculator.main(null);
References/Citations
Altadmri, A., & Brown, N. C. C. (2015). 37 million compilations: Investigating novice programming mistakes in large-scale student data. In Proceedings of the 46th ACM Technical Symposium on Computer Science Education (pp. 522–527). Association for Computing Machinery. https://doi.org/10.1145/2676723.2677258
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf
Ma, L., Ferguson, J., Roper, M., & Wood, M. (2007). Investigating the viability of mental models held by novice programmers. In Proceedings of the 38th SIGCSE Technical Symposium on Computer Science Education (pp. 499–503). Association for Computing Machinery. https://doi.org/10.1145/1227310.1227481
Oracle. (n.d.). Scanner (Java SE 21 & JDK 21). Java Platform, Standard Edition 21 API specification. Retrieved September 17, 2026, from https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Scanner.html