1.4 Assignment Statements and Input
In this lesson, we will explore assignment statements (how values are stored into variables) and input (how programs can receive information from the user). Both of these are fundamental to programming in Java and will help you understand how data flows in a program.
We’ll start by learning the concepts, then apply them in code examples, integrate them into a menu system, and finally reinforce understanding with an interactive quiz game.
Learning Objectives
- LO 1.4.A: Develop code for assignment statements with expressions and determine the value stored in variables.
- LO 1.4.B: Develop code to read input.
By the end of this lesson, you should be comfortable with assigning values to variables, updating them, and using the Scanner
class to capture input from the keyboard.
Essential Knowledge
- 1.4.A.1: Every variable must be assigned a value before it can be used. The type of the value must match the variable’s declared type. Reference types can be assigned an object or
null
. - 1.4.A.2: The assignment operator
=
is used to store the result of the right-hand expression into the left-hand variable. - 1.4.A.3: During execution, an expression is evaluated into a single value before assignment occurs.
- 1.4.B.1: Input in Java is often handled by the
Scanner
class, which allows reading text, numbers, and other data from the keyboard.
1. Assignment Statements
Variables in Java are like containers that hold values. First, you must declare them with a type (e.g., int
, String
), and then you can assign a value. If you reassign the variable later, the previous value is overwritten.
// Example 1: Assigning different variables
// Assign a String
String name = "Billy";
// Assign an Integer
int age = 10;
// Assign a Double
double height = 165.34;
// Assign a Char
char firstLetter = 'B';
// Assign a Boolean
boolean isStudent = true;
// Example 2: Reassignment
int x = 10;
x = 20; // the old value (10) is replaced by the new value (20)
// Example 3: Reference and null
String name = null; // variable declared but currently points to nothing
name = "Ahaan"; // now it refers to a String object with value "Ahaan"
2. Input with Scanner
So far, we’ve hardcoded values into variables. But often, we want programs to react to user input. Java provides the Scanner
class for this purpose. With it, we can ask users for information and store their answers in variables.
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
System.out.print("Enter a number: ");
int num = scanner.nextInt();
System.out.println("You entered " + num);
3. Menu Integration (Menu.java)
In larger projects, lessons or programs are often tied together with a menu system. This makes it easy for users to select which program to run. We’ve extended Menu.java
to include our new InputLesson
.
MenuRow[] rows = new MenuRow[]{
new MenuRow("Exit", () -> main(null)),
new MenuRow("Output Lesson", () -> OutputLesson.main(null)),
new MenuRow("Input Lesson", () -> InputLesson.main(null)), // NEW LESSON
new MenuRow("Do Nothing", () -> DoNothingByValue.main(null)),
new MenuRow("Swap if Hi-Low", () -> IntByReference.main(null)),
new MenuRow("Matrix Reverse", () -> Matrix.main(null)),
new MenuRow("Diverse Array", () -> DiverseArray.main(null)),
new MenuRow("Random Squirrels", () -> Number.main(null))
};
4. InputLesson.java
Here is the complete Java program for our Input lesson. It demonstrates reading strings, integers, and doubles from the user. Notice how each type of data uses a different method (nextLine
, nextInt
, nextDouble
).
package com.open.spring.hacks.methodsDataTypes;
import java.util.Scanner;
public class InputLesson {
public static void main(String[] args) {
System.out.println("=== Input Lesson ===");
Scanner scanner = new Scanner(System.in);
// Example 1: String input
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
// Example 2: Integer input
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Next year you will be " + (age + 1));
// Example 3: Double input
System.out.print("Enter a decimal number: ");
double d = scanner.nextDouble();
System.out.println("Half of that is " + (d / 2));
}
}
5. Key Takeaways
- Assignment (
=
) stores values into variables. - Variables must be declared with a type before use.
null
means a reference variable points to no object.- The
Scanner
class allows programs to read input from the keyboard. Menu.java
shows how lessons can be integrated into a bigger program.
Practice
Let’s check your understanding with a few multiple-choice questions.
Q1. What is the value of x
after this code?
int x = 4;
x = x + 3;
- A. 4
- B. 7
- C. 3
- D. null
Answer: B
Q2. Which statement correctly declares and initializes a String
variable?
- A.
String s = 5;
- B.
String s = null;
- C.
String s = "Hello";
- D. Both B and C
Answer: D
7. Hack & Blog Ideas
- Hack: Modify
InputLesson
so it asks the user for three numbers and outputs their average. - Blog: Write a short explanation of the difference between assignment (
=
) and comparison (==
) in your own words. - Portfolio: Add screenshots or a demo video showing your program running in
Menu.java
.
8. Interactive Quiz Game
Finally, here’s a fun quiz game built in JavaScript + HTML to test your knowledge of assignment statements, input, and how they connect to the lesson. Play through it and check your understanding!