Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAbhijeet Pednekar
Courses Plus Student 1,566 PointsAdd another if statement that checks if the firstExample is equal ignoring case to thirdExample.
Add another if statement that checks if the firstExample is equal ignoring case to thirdExample. If it, is print out "first and third are the same ignoring case".
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
{if(firstExample.equals("hello")==
secondExample.equals("hello"))
console.printf("first is equal to second");}
{if(firstExample==
thirdExample.equalsIgnoringCase("hello"))
console.printf("first and third are the same ignoring case");}
2 Answers
Steve Hunter
57,712 PointsHi there,
The equals
method works for all strings. It takes one parameter; another string (or variable containing one). The method compares the string the method is applied to with the parameter passed. In our example, we're comparing firstExample
with secondExample
. That look like:
firstExample.equals(secondExample)
That code will evaluate to a true
or false
and so is perfect to be used with an if
conditional test:
if(firstExample.equals(secondExample)){
// this hapens if the result is true
console.printf("first is equal to second");
}
The next test is slightly different as it ignores the case of both strings. In the code you've written in your question and in the answer by Cody, the method has been named wrongly. It is equalsIgnoreCase()
. You use it in exactly the same way as equals()
. The finished challenge looks like:
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if(firstExample.equals(secondExample)){
console.printf("first is equal to second");
}
if(firstExample.equalsIgnoreCase(thirdExample)){
console.printf("first and third are the same ignoring case");
}
I hope that helps.
Steve.
Cody Te Awa
8,820 Pointsif (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
if (firstExample.equalsIgnoringCase(thirdExample)) {
console.printf("first and third are the same ignoring case");}