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

Riya Prasannan
Riya Prasannan
806 Points

equalsIgnoreCase

String firstExample = "hello"; String secondExample = "hello"; String thirdExample = "HELLO"; if(firstExample.equalsIgnoreCase("HELLO")) { console.printf("First and third are the same ignoring case"); }

Not able to understand what is wrong in this.

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.equalsIgnoreCase("HELLO"))
{
  console.printf("First and third are the same ignoring case");
}

2 Answers

Wil Arter
Wil Arter
1,381 Points

Riya,

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

The above worked, the task says to compare the firstExample to the thirdExample variable however in your answer you are not comparing these, you are comparing the firstExample to a string of "HELLO" instead of the variable thirdExample, hope this helps.

Riya Prasannan
Riya Prasannan
806 Points

I tried this too, but it wasnt working

Hi there,

Just to add, you need to keep the first task's if statement in place else this will fail the challenge. The second task asks for another if statement so just add to the bottom of your code:

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");
}

Steve.

Riya Prasannan
Riya Prasannan
806 Points

Hi Arter and Steve, I tried the below code again and its working now. Not sure what happened when I tried earlier. if(firstExample.equalsIgnoreCase(thirdExample))

Appreciate the help and quick reply. Thanks.