1.04 Assignment Statements and Input
Store values in variables using assignment and read user input with Scanner.
1. 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 & Test: Lesson authors: add what happened in your trial run and what you changed.
2. 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
For Hack 1: fill in the blanks, then run it
View IPYNB Source
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
From the College Board
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.
3. Reference Guide
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")
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().
4. 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: 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);
5. 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 open the answer.
- 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 on paper, writing what each variable holds after every line. Then open the answer.
Task: Code Trace
Question: Trace through the complete example. If the user enters “Alice Wonderland”, 20, and 3.85, what is the exact output?
Answer
=== User Information Program === Enter your full name: Alice Wonderland Enter your age: 20 Enter your GPA: 3.85 --- Summary --- Name: Alice Wonderland Age: 20 (next year: 21) GPA: 3.85MCQ 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);
6. 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 & Feedback Evidence
Feedback Received: Lesson authors: what your peers said in the practice run.
Revision Made: Lesson authors: what you changed because of it.
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
Need to update a submission later? Open the submissions dashboard.