1.04 Assignment Statements and Input
Tell the assignment operator = apart from ==, trace how assignment statements change variables, and read keyboard input with Scanner.
1. 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.
2. 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.
3. 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.
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.
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)
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Write your own trace before you run the Popcorn cell.
- Run each cell and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller. - Made-up names, ages, and GPAs are fine; do not share anything you would not want public.
Popcorn Hack (In-Class)
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 submission notes
Write what each variable holds after every line.
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.
4. 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 submission notes.
5. 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 == (5 minutes)
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 submission notes.
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 (5 minutes)
Input. Read three integers with Scanner. Replace each 0 with the method from Picking a Scanner Method that reads a whole number.
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 (10 minutes)
Combined. Read three integers, average them, and print the result with two decimals. Watch for integer division. Skeleton below.
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);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | The trace written in the comment matches the run output, including the (next year: ...) line. The Popcorn checker line is in the notes. |
| MCQ | 0.2 | 4 correct. 0.15 for 2 or 3, 0.1 if every question was answered. |
| Homework Hack 1 | 0.15 | = stores 90, == checks it, all three checks print true, and the notes explain the difference and a bug from mixing them up. |
| Homework Hack 2 | 0.2 | Reads three integers with Scanner and the check prints true. |
| Homework Hack 3 | 0.25 | Averages them without integer division, shows two decimals, and both checks print true. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - Popcorn trace in the comment matches the run output, including the
(next year: ...)line. - Popcorn checker line in the notes.
- MCQ score in the notes.
- Hack 1: the
=versus==explanation and the bug are in the notes. - Hack 3: a
doublein the division, two decimals. - Every homework check prints
true.
6. LxD Cycle Process
Empathize: Students read x = x + 5 as a math equation instead of “work out the right side, then store it.” At the end of a first-year Java course, about a third of students still held a broken model of assignment (Ma et al., 2007). Mixing up = and == is one of the most common novice mistakes (Altadmri & Brown, 2015).
Define:
- POV: CSA students need a clear picture of assignment and of where input comes from, because every interactive program depends on both.
- Learning Goal: Students will write assignment statements and say what each variable holds, and read int, double, and String input with Scanner.
Ideate:
- HMW Question: How might we make “right side first, then store” automatic through line-by-line tracing?
- HMW Question: How might we show that a value can come from outside the program: a keyboard now, a web request in the project?
- Activity: Trace a full input program line by line, predict answers before revealing them, then use = and ==, read input, and build an average calculator.
Prototype:
- A reference guide on Scanner methods and the nextInt() and nextLine() problem, runnable input examples with simulated answers, a code trace, an MCQ knowledge check, three scaffolded homework hacks with self-checking output, and a grading rubric.
- Students write their trace before running the cell and revise it where it differs.
- Excellence means tracing what each variable holds after every line, avoiding integer division, explaining
=versus==, and improving the first attempt rather than only producing working code.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers work out the right side first before storing, and whether they trace before they run.
- Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from Popcorn traces, runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
7. 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
Submit Assignment
Need to update a submission later? Open the submissions dashboard.