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.
- Always print
Password:followed by the typed password on the first line. - If the password matches
open123exactly (capitals, digits and all), printAccess grantedon the next line. - If it does not match, print
Access deniedon the next line. - The comparison is case sensitive:
Open123is not the same asopen123. - 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
- typed: open123
It prints
Password: open123 Access granted
an incorrect password (rule 3)
You type
- typed: wrong
It prints
Password: wrong Access denied
wrong case: denied (rule 4)
You type
- typed: Open123
It prints
Password: Open123 Access denied
Exercise 2.6.22.6.2 · Write itCommand check
Write a program that reads a command (text) and responds.
- If the command is
help, printCommands: help, quit. - If the command is
quit, printGoodbye. - For any other command, print
Unknown:followed by the command. - Print exactly one of those three lines. Spaces at the end are ignored.
- The comparison is case sensitive:
Helpis not the same ashelp.
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
- typed: help
It prints
Commands: help, quit
the quit command (rule 2)
You type
- typed: quit
It prints
Goodbye
an unknown command (rule 3)
You type
- typed: hello
It prints
Unknown: hello