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 Censoring Words - Using String Equality

Klaudia Krol
Klaudia Krol
576 Points

if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to help?

Hi Dear Treehouse friends ,

Could you please have look whats rong with my code ?

Thanks , Klaudia

Santos Solorzano
Santos Solorzano
3,125 Points

Hi Klaudia,

Are you using the equals() method to see if firstExample is equal to secondExample?

7 Answers

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

That's very strange. Because if I paste in this code in the challenge, it passes both steps. Keep in mind that I have not altered the segment of code that I posted previously.

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

First, make sure you understand this code and what it does. Then copy/paste this into the challenge (to make sure all the spelling is exactly the same and no stray punctuation marks). If it still doesn't pass, then you may need to contact support at the link at the top of this page. Good luck! :sparkles:

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

Hi there Klaudia Krol! Santos Solorzano is correct. You need to be using the .equals() method to compare the strings. You've asked us to point out what's incorrect in your code, however, your code isn't posted here. So I'm going to post the section you need for the first step and then walk you through it.

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

Here we set up an if statement to see if firstExample is equal to secondExample. As mentioned previously, we use the .equals() method to accomplish this. If the result is true, then we use console.printf to print out the string as specified by the challenge instructions. Hope this helps! :sparkles:

Klaudia Krol
Klaudia Krol
576 Points

Thank you all for help :-)

Best code practise

Ikuko Schoeller
Ikuko Schoeller
6,116 Points

I know it is too late too say. However I had a same problem. I could not find any mistakes. I closed the browser then started over again. Then the code was accepted. Whenever something wired happen, I usually close the browser or clear the cash. Sometimes it solve the problem!

Diana Ci
Diana Ci
18,672 Points

my problem with this code challenge was that I added System.exit(0); and the code doesn't work with it :)

Idk if this helps but this is the code i used and it passed the first and second task Just make sure you use the right string method to ignore the case on the second task

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

Steven Parker
Steven Parker
231,184 Points

:x: This doesn't seem to be a legitimate (and certainly not correct) answer!