003-002-012
Comparing Strings
Medium
Problem Description
In this problem, you will create a program that reads two strings from standard input, compares them using both == and equals(), and displays the results to standard output.
Comparing Strings
Learning Objective: Understand why equals() should be used instead of == for string comparison
Overview
Verify the difference between == and equals() when comparing strings read via Scanner.
Input Format
string1
string2
Specifications
- Read two strings from standard input, one per line
- Output comparison result using == (Scanner always creates new String objects, so always false)
- Output comparison result using equals() (true if contents match, false otherwise)
Output Format
Using ==: false
Using equals(): true
Example
Input:
Hello
Hello
Output:
Using ==: false
Using equals(): true
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up