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

foxtails
foxtails
3,655 Points

firstExample equal to thirdExample. I'm stuck.

I can't find the error. First statement worked. This doesn't.

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");
}
Dmytro Konkov
Dmytro Konkov
533 Points
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

if (firstExample.equalsIgnoreCase ("hello")); {    // <-- incorrect comparison & incorrect semi-colon here
     console.printf("first is equal to second");
}
if (firstExample.equalsIgnoreCase ("HELLO")); { // <-- incorrect comparison  & incorrect semi-colon here
     console.printf("first and third are the same ignoring case");
}

[MOD: edited code block & added code comments - see answer below]

foxtails
foxtails
3,655 Points

Still don't see it. And the semi colon is not needed in Workspace code that worked. Tried it anyway didn't work.

4 Answers

Hi Foxtails ...

You need to compare the two variable contents, not a string 'literal' like "HELLO".

So, rather than comparing firstExample to "HELLO", compare it (ignoring case) to thirdExample. That is the variable that contains "HELLO".

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 is equal to third");
}

I hope that helps.

Steve.

Hi there,

You do need to leave all the previous code intact, so your end result should have at least 9 lines of code, like my answer did.

What error are you getting?

Steve.

foxtails
foxtails
3,655 Points

I have corrected the problem you both pointed out, but I still am stuck. It won't accept it. Here is my coding:

if(firstExample.equalsIgnoreCase(thirdExample)){
  console.printf("first is equal to third");
}
foxtails
foxtails
3,655 Points

Hi Steve, you just DID IT! That was it. I did not leave the previous example, but modified it to fit the challenge for the next task. And that caused the problem. Thank you so much!

Excellent! Glad you got it fixed.

You will rarely need to delete code from earlier tasks; best just leave it there unless the challenge says specifically not to.

:smile: :+1: