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 trialJay Lee
Courses Plus Student 196 PointsWhat should I put?
question: Add an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to second". what should i do?
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample = secondExample) {
console.printf("first is equal to second\n");
1 Answer
Steve Hunter
57,712 PointsHi Jay,
You should compare for equality of a string using the .equals()
method. For other datatypes you can compare with a double-equals, ==
, but not for Strings. Use dot notation on the first string variable and pass in the second string variable as the parameter to the .equals()
method, i.e. put the second variable name inside the parentheses.
You also need to close your curly braces after the semi-colon.
Try something like:
if (firstExample.equals(secondExample)){
console.printf("first is equal to second\n");
}
I hope that helps,
Steve.