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

Daniel Dahlman
PLUS
Daniel Dahlman
Courses Plus Student 621 Points

What should I do?

What should I do? It says I have to add equalsIgnoreCase which I do but still it says its wrong

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
if (firstExample.equalsIgnoreCase("hello")) {
  console.printf("first and third are the same ignoring case");
}
String secondExample = "hello";
String thirdExample = "HELLO";

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Daniel Dahlman Hi there! You're doing well, and you clearly made it past the first challenge without problem. But there's a couple of things going on here. You're supposed to be comparing firstExample against thirdExample. Here, you're comparing it against the string literal "hello". Secondly, you've removed the code from the first step, which could cause problems with the challenge. And third, even if you changed it to compare against the thirdExample right now, it would fail. Why is that? Because you're running your if statement before the third example is even declared and initialized. Here's how my code looks at the end of the second step.

// 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(secondExample)) {
  console.printf("first is equal to second");
}

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

The first if statement compares firstExample to secondExample and makes it case sensitve. For instance, if secondExample had been equal to "Hello" then the console.printf line wouldn't happen as it would evaluate to false.

The second if statement compares firstExample to thirdExample ignoring case. In this case, it will evaluate to true and the console.printf will execute. Hope this helps! :smiley: