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

I don't get it why this should be wrong?

Why should this be wrong and why I get an error message?

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = "hello";
if (firstExample.equals("secondExample")){
  console.printf("first is equal to second");
  System.exit(0);
}
String secondExample = "hello";
String thirdExample = "HELLO";

3 Answers

You have used variables before you have declared them in your code.

So, move the whole if statement below the variable declarations.

// 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");
  System.exit(0);
}

That way, the compiler knows about secondExample before you use it in your code. (and see the comment below)

Steve.

You also need to remove the double quotes from around "secondExample" - it is a variable, not a string.

Make sense?

Steve.

wtf? This won't work.. :D?

            // 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");
  System.exit(0);
}

Odd. Looks fine - let me get into the challenge - I'll be right back!

Sorry - that's me missing your added line. You don't need the System.exit(0) at all. Delete that line.

Apologies for not seeing that before.

Steve.

I get it. Thanks Steve for the quick help. :)

No problem! :-)

Sorry - that's me missing your added line. You don't need the System.exit(0) at all. Delete that line.

Apologies for not seeing that before.

Steve.