UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.4
1.4 Assignment Statements and Input
Assignment stores a value; the revised course also expects you to know how a program reads input, using the Scanner class methods on the reference sheet.
What you need to know
=is assignment: evaluate the right side, store it in the variable on the left. It is not equality (that's==).- The right side is evaluated using the variable's current value, so
x = x + 1increments. The expression must produce a value compatible with the variable's type. - Assigning one primitive variable to another copies the value. Assigning one reference variable to another copies the reference — both then point to the same object.
- Input comes from
Scanner. Create one withScanner input = new Scanner(System.in);then read withnextInt(),nextDouble(),nextLine(), ornext()(one word). hasNext(),hasNextInt(),hasNextLine()return whether more input is available — used to loop over input of unknown length.- The nextInt/nextLine trap:
nextInt()leaves the newline in the buffer, so a followingnextLine()returns an empty string. The fix is an extranextLine()to consume it. - Swapping two variables requires a temporary:
int temp = a; a = b; b = temp;
Worked example
Scanner in = new Scanner(System.in);
System.out.print("Age: ");
int age = in.nextInt();
in.nextLine(); // consume leftover newline
System.out.print("Name: ");
String name = in.nextLine();
System.out.println(name + " is " + age);
Without line 4, if the user types 17 and Enter, name becomes the empty string because nextLine() reads the leftover newline. This exact trap is the reason Scanner questions exist.
Exam tip: Questions about Scanner are usually "which method reads a whole line vs. one token" (
nextLine vs next) or the nextInt-then-nextLine trap. Also remember: = assigns; a question with if (x = 5) is a compile error because an int isn't a boolean.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Assignment is right-to-left. Evaluate the entire right side using current values; then store in the left variable.
x = x + 1is not an equation; it's "compute x + 1, then overwrite x." - Copying primitives copies values. After
int b = a;, a and b are independent. Copying references copies addresses. AfterDog b = a;, both point at one Dog. (Full treatment in 1.13.) - Scanner reads tokens or lines.
next()reads one whitespace-delimited token.nextLine()reads to the end of the line, including spaces.nextInt()/nextDouble()read one token and convert it — and throwInputMismatchExceptionif it isn't a number. - The buffer trap in detail: the user types
42⏎.nextInt()consumes42and leaves⏎. The nextnextLine()reads up to that⏎— an empty String — and consumes it. Only the followingnextLine()gets the actual next line. Fix: callnextLine()once afternextInt()to discard the leftover. - hasNext family:
hasNext(),hasNextInt(),hasNextDouble(),hasNextLine()return booleans and don't consume anything. They're how you loop over input of unknown length — and they're the same methods used for files in 4.6. Scanneris injava.util— needs an import. Creating one:new Scanner(System.in)for keyboard.- A swap needs a temporary variable. The three-line pattern is worth memorizing because it appears inside sorting algorithms in Unit 4.
Mistakes that cost points
- Using
==for assignment or=for comparison.if (x = 5)won't compile (int isn't boolean).x == 5;compiles but does nothing. - Forgetting the buffer clear after nextInt. The next nextLine() gets an empty string. This is the single most common Scanner bug and it's tested.
- Using next() when you need a whole line.
next()stops at the first space. "Ram Singh" becomes "Ram". - Two-line swap.
a = b; b = a;leaves both equal to b's original value.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 After the following code runs, what are the values of
a and b?
int a = 3; int b = 7; a = b; b = a;
Show answer
Answer: C. a becomes 7, then b is assigned a's new value, 7. No swap happened — a temp variable was needed.
Q2 A program reads an integer with
nextInt() and then immediately calls nextLine() to read the user's name. The name is stored as an empty string. Which of the following best explains why?Show answer
Answer: B. This is the standard Scanner buffer behavior. An extra nextLine() call fixes it.
Key vocabulary
- Assignment (=)
- stores the value of the right-side expression in the left-side variable
- Scanner
- a class for reading input from the keyboard or a file
- nextLine()
- reads an entire line of input as a String
- nextInt()
- reads the next integer token, leaving the newline in the buffer