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 can't tell what is wrong with my code. The syntax says there is something wrong with my if statement line.

Thanks for helping.

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

2 Answers

Hi Charles,

A couple of things amiss there.

First, you've used variable names before they've been declared so you need to move your if statement below the definitions.

Next, you've put secondExample inside of double-quotes. That makes it a string like "Steve" or "Wednesday" - to use the value stored inside the variable named secondExample, skip the quotes.

I think that should see you fine. Else shout back!

Steve.

There's a rogue semi-colon in there too:

So to pick up where Stone Preston left off:

String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

if (firstExample.equals(secondExample));{  // <-- omit the semi-colon and the double quotes
  console.printf("first is equal to second");
}

I hope that helps.

Steve.

Thanks for the help!

No problem - I hope it all worked for you.

Steve.

Stone Preston
Stone Preston
42,016 Points

you need to put the if statement AFTER the strings are created and change the string literal in the equals method to the secondExample variable name

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