1.04 Assignment Statements and Input
Store values in variables using assignment and read user input with Scanner.
1. Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Assignment statement | Stores the value of the right side in the variable on the left. | x = x + 5; |
| Assignment operator | = means “store,” not “is equal to.” |
score = 90; |
| 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 |
| Input | Data that comes from outside the program. | Keyboard, a web request |
| Scanner | A class that reads text input. | Scanner sc = new Scanner(System.in); |
- 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().
2. 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 build an average calculator or explain = versus ==.
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, a choice of two homework hacks, 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.
3. 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.
4. Lesson Plan
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().
Tech Talk (5 minutes)
Assignment works right to left. Java finishes the whole right side first, then stores the result on the left.
Code Runner Challenge
Run it with the simulated answers, then change them
View IPYNB Source
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:
Code Runner Challenge
Trace the output first, then run it to check
View IPYNB Source
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.
Code Runner Challenge
Write your trace in the comment first, then run it
View IPYNB Source
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
5. Code Examples
A. Simple: Reading Input
The runner has no keyboard, so this copy reads the answers from a String. Change them and run it again.
Code Runner Challenge
For Hack 1: fill in the blanks, then run it
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. Complex: Full Input Program
These are the Popcorn Hack inputs. Trace the output first, then run it.
// 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);
6. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter:
---
layout: post
codemirror: true
title: Assignment Statements and Input HW
categories: [Java]
lesson_language: Java
lesson_topic: Assignment-and-Input HW
lesson_part: interactive
lesson_type: lesson
permalink: /csa/unit_01/1_4-hw
author: yourGithubID
---
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 1.4 Assignment Statements and Input
MCQ 1.4: <paste the copied result line, such as 5/6 | answers: C,B,C,B>
Popcorn trace: Name: Alice Wonderland / Age: 20 (next year: 21) / GPA: 3.85
Homework hack chosen: <Hack 1 or Hack 2>
Hack 1: three integers read, average = <value> with two decimals
Hack 2: = stores, == compares, bug explained = <one line>
Submission Safety Rules (Read First)
- Write your own trace before you run the Popcorn cell.
- Submit one hack only, and say which one.
- Run Hack 1 and leave the output showing.
- Include your MCQ score.
- Use
##headings or smaller.
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
Write what each variable holds after every line.
// 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);
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.
Homework Hack
Pick one and do it in your submission notebook.
Hack 1: Three-number average. Read three integers, average them, and print the result with two decimals. Watch for integer division. Skeleton below.
Hack 2: = versus ==. Explain the difference between = and ==, show a code example of each, then explain a bug that happens when they get mixed up.
// CODE_RUNNER: For Hack 1: fill in the blanks, then run it
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);
}
}
AverageCalculator.main(null);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | The trace includes Age: 20 (next year: 21) and the other two summary lines. |
| MCQ | 0.2 | 5 or 6 correct. 0.15 for 3 or 4, 0.1 if every question was answered. |
| Hack 1: input | 0.2 | Reads three integers with Scanner. |
| Hack 1: average | 0.25 | Averages them without integer division. |
| Hack 1: output | 0.15 | Shows the average with two decimals. |
| Hack 2: explanation | 0.2 | Explains that = stores a value and == compares two values. |
| Hack 2: examples | 0.25 | Code examples of each operator. |
| Hack 2: bug | 0.15 | Explains a bug caused by confusing them. |
| Total | 1.0 |
Quick Validation Checklist
- Popcorn trace written out, including
(next year: 21). - MCQ score in the notes.
- The chosen hack is labeled (do one hack only, 0.6 total).
- Hack 1: three
nextInt()calls, a cast todouble, two decimals. - Hack 2: both
=and==in code, plus the bug explained.
7. Lesson Revisions
Revision Made: Lesson authors: what you changed because of it.
8. Feedback Evidence
Feedback Received: Lesson review of CSA 1.1 to 1.4, September 29, 2026.
- Reference Guide (strength): The only guide that explains a common bug: the
nextInt()andnextLine()problem, why it happens, and the fix, backed by Oracle’s docs. - Reference Guide (fixed): Covered only the input half of the lesson. Assignment, the first half of the topic, appeared only in the Tech Talk.
- Game (open): The 1.3 and 1.4 game is linked here, but the Grading Plan gives it no points.
- Grading Plan (open): The MCQ row gives full credit for “5 or 6 correct,” but the MCQ only has 4 questions, so full credit is impossible. The sample score line
5/6has the same problem. - Structure (fixed): The section order did not match the 1.2 model lesson. The Reference Guide came third, the College Board quotes sat inside the Lesson Plan, the Grading Plan was its own section, and Revisions and Feedback were combined.
- Frontmatter (fixed): Missing keys that the 1.2 model has, including
assignment_submission_type,assignment_creator_uids, andGroup.
9. References
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
Self-Assessment
Rate your work on this assignment, then add a short reflection. All 5 fields are required before you can submit.
Need to update a submission later? Open the submissions dashboard.