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 trial

Java Java Basics Perfecting the Prototype String Equality

philip williams
philip williams
5,991 Points

I don't understand the question at all, can anyone help? .equals

"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 don't understand at all, it says add another if statement but i didn't have to add a if statement in the first part of the challenge to get to task 2. Any help would be greatly recieved

Phil

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";


console.printf ("first is equal to second");
if (first.equalsIgnoreCase);

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Your print statement needs to go inside the if clause.

if (firstExample.equals(secondExample)) {
  console.printf ("first is equal to second");
}

You could do the same in step 2, by comparing firstExample to thirdExample with the help of equalsIgnoreCase() method.

if (firstExample.equalsIgnoreCase(thirdExample)) {
  console.printf("first and third are the same ignoring case");
}

Hope it helps

Note: Answer updated thanks to the comment posted by Steve.

Hi there,

Just one point on William's speedy answer; we shouldn't really use == to check for string equality.

The reasons are covered in Craig's course, The Thing About Strings and are also explained better than I'm able to in this post on S.O.. There's another TH thread here too.

While you can probably get through a challenge using ==, it is better to use the .equals() method to check for logical equality, rather than testing for the two things being the same in memory.

It's a small point, but one worth making as it is certainly best practice.

Steve.

William Li
William Li
Courses Plus Student 26,868 Points

Hi Steve, thanks for the clarification of a very important point. Guess my brain is still in C# mode.