Skip to content
ExamSpine

Lesson 2.6

Comparing text

Part 2: DecisionsReading, about 3 min2 exercises in this lesson

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Comparing text

By the end of this you will be able to compare text in a condition, correctly in both languages.

Lesson 2.2 mentioned that Java uses .equals for text, not ==. This lesson practises it.

Comparing text

This program prints Confirmed only when the user types yes:

answer = input()
if answer == "yes":
    print("Confirmed")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String answer = scanner.nextLine();
    if (answer.equals("yes")) {
        System.out.println("Confirmed");
    }
}

In Python, == compares the characters, so answer == "yes" is true when the user typed yes.

In Java, == on text checks whether two variables point to the same object in memory, not whether the characters are the same. For text read with scanner.nextLine(), answer == "yes" is usually false even when the user typed exactly yes. For text written straight into the program, it can be true by accident, which makes the bug hard to find. That is why Java uses answer.equals("yes"). Always compare text with .equals, never ==.

Capitals matter

The comparison is case sensitive in both languages: "quit" and "Quit" are not equal, and neither are "yes" and "YES".

A common mistake: forgetting that capitals matter. If you want to accept both quit and Quit, convert the typed text to lower case first (a later lesson teaches how).

Now try it yourself on the questions below.

Exercise 2.6.12.6.1 · Write itPassword check

Write a program that reads a password (text) and checks whether it matches the secret password open123.

  1. Always print Password: followed by the typed password on the first line.
  2. If the password matches open123 exactly (capitals, digits and all), print Access granted on the next line.
  3. If it does not match, print Access denied on the next line.
  4. The comparison is case sensitive: Open123 is not the same as open123.
  5. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use == to compare text. In Java, use .equals (never == for text).

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • the correct password (rules 1 and 2)

    You type

    1. typed: open123

    It prints

    Password: open123
    Access granted
  • an incorrect password (rule 3)

    You type

    1. typed: wrong

    It prints

    Password: wrong
    Access denied
  • wrong case: denied (rule 4)

    You type

    1. typed: Open123

    It prints

    Password: Open123
    Access denied
solution.pySolution.java

Exercise 2.6.22.6.2 · Write itCommand check

Write a program that reads a command (text) and responds.

  1. If the command is help, print Commands: help, quit.
  2. If the command is quit, print Goodbye.
  3. For any other command, print Unknown: followed by the command.
  4. Print exactly one of those three lines. Spaces at the end are ignored.
  5. The comparison is case sensitive: Help is not the same as help.

Your program is run several times. Only what it prints is checked.

In Python, use == and elif. In Java, use .equals and else if.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • the help command (rule 1)

    You type

    1. typed: help

    It prints

    Commands: help, quit
  • the quit command (rule 2)

    You type

    1. typed: quit

    It prints

    Goodbye
  • an unknown command (rule 3)

    You type

    1. typed: hello

    It prints

    Unknown: hello
solution.pySolution.java