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 trialMichael Sothan
8,048 PointsHere 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.
// 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
Philip Gales
15,193 PointsThe 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 offirstExample
and then calledconsole.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
8,048 PointsMichael Sothan
8,048 PointsGreat. 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