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

Zeeshan Somani
Zeeshan Somani
323 Points

Can someone tell me what is wrong with this code? I am trying this in the interactive session but cannot figure it out.

The point of this code is to output "first is equal to second if the contents of the 2 string are the same. Any ideas?

String firstExample = "hello"; if (firstExample.equals (secondExample)) { console.printf ("first is equal to second."); } String secondExample = "hello"; String thirdExample = "HELLO";

1 Answer

Hi Zeeshan,

The first part of the challenge should look like this:

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

Is this what you meant?

-Rich

Zeeshan Somani
Zeeshan Somani
323 Points

That is exactly what I wrote. The only difference is the placement. I placed the if statement between the first and second string. I ran a similar program in workshop so the method should work but the interactive question isn't accepting it. Is the placement at fault?

Hi Zeeshan,

Yes you will need to make sure your condition appears after the strings that are set. Think of it as though they can't be compared if they don't exist yet :)

Hope that helps.

-Rich

Zeeshan Somani
Zeeshan Somani
323 Points

Perfect. Works now. Thanks.

No problem :)

-Rich

Aamir Mirza
Aamir Mirza
25,077 Points

Thanks Rich! I was having trouble with this too. I had the exact same code as you except I put System.exit(0); at the end of the if block. Hopefully that's a bug x_x