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

Lyndsi Barfield
Lyndsi Barfield
633 Points

I am stuck on this question about equalsIgnoreCase. I'm not sure what to do differently to get it right.

its the second if statement that I have wrong.

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

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

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

2 Answers

The line

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

should look like this :

 if (firstExample.equalsIgnoreCase(thirdExample) {
  console.printf("first and third are the same ignoring case");
}
Jason Anders
Jason Anders
Treehouse Moderator 145,860 Points

Hi Rumyana Dimitrieva

First, it's proper practice in the Community to provide a "Why" when giving answers to questions here. We appreciate your participation in the Community, but please, in the future, provide an explanation as to why the code is incorrect and/or why your provided code is correct.

Second, you above code has a syntax error. You are missing the closing parenthesis for the if statement

Thank you. :dizzy:

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Lyndsi,

There are actually a couple things going on here. The first one is correct, as you are directly comparing them to each other using the comparison operator (==).

But, for the second task, you need to use an instance method equalsIgnoreCase() which needs parentheses and takes an argument. Here, you can't use the comparison operator, because you are using a method that does that for you behind the scenes.

You are correct, in that you access the method using dot notation, but instead of == you pass what you want to compare as an argument inside the parentheses like so:

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

If you still uncertain about the 'why' with this syntax, just take a quick review of the video before moving on.

Keep Coding! :) :dizzy: