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

Michael Sothan
Michael Sothan
8,048 Points

Here was my response: if firstExample.equals("secondExample")) { console.printf("first is equal to second"); }

What is wrong with my response?

So far we have only learned how to check if the value of a string is equal to a fixed value but not to another string. No idea how to run that kind of check. Please help.

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.equals("secondExample")) {
  console.printf("first is equal to second");
}

1 Answer

The first problem we have is a compiler error.

Bummer! There is a compiler error. Please click on preview to view your syntax errors!

So we click preview and get the following:

JavaTester.java:108: error: '(' expected

if firstExample.equals("secondExample")) {

^

1 error

This tells us you are missing a '(' right after the if statement. This makes sense, you did put the closing one so this was probably a mistake.

Now we get the following error:

Bummer! Are you sure you used the equals method of firstExample and then called console.printf?

This one is because you are comparing 'firstExample' with a literal string. I am going to remove the quotation marks from secondExample.

Finally, your code for task 1 is:

// 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)) { //<--------both corrections are on this line.
  console.printf("first is equal to second");
}
Michael Sothan
Michael Sothan
8,048 Points

Great. Thanks! Yeah, the parenthesis at the beginning was actually just a copy/paste error I made while I was going back and forth troubleshooting. My critical mistake was the quotation marks on secondexample.

Cheers