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 trialmamadou diene
Python Web Development Techdegree Student 1,971 PointsAdd an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal t
i don't know why its not working
// 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");}
4 Answers
Jennifer Nordell
Treehouse TeacherHi there! Ari Misha is partially correct. You are doing an assignment and that will be incorrect. And the code given in his answer does pass the challenge.
However, I'm of the opinion that it technically shouldn't. Unlike many other languages, in Java when we're comparing String
s we need to use the .equals
method.
The correct code that will also work outside the challenge would be:
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
Hope this helps!
Ari Misha
19,323 PointsHiya Mamadou! You need to check for loose equality "==" , not making any assignments. So in your "if" conditional, replace "=" with "==". Here is how your code should look like:
// 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");
}
Daniel Burt
3,699 PointsHi all. I've just been working through this, and I have to say it's slightly confusing bearing in mind the video that we watch prior to it.
The video uses the example of the word "dork" to perform its equals function. The code challenge then uses what feels like a more complex concept (bearing in mind we are complete beginners) of comparing strings and asking if they themselves are equal.
I ended up getting Ari's answer in the end, because I Googled (as suggested in the video) 'equals in Java' and the first result is ==, and this was because my brain was thinking that I'd missed something in the previous video about comparing strings to each other for equals rather than querying:
if (noun.equals("dork")) {
I wonder if the wording of the challenge might be made a little clearer?
Namara Kitto
455 PointsIf this question is in the beginning of the strings module, the if statement needs to be placed at the end, because you need to have the three variables initialized and declared first. If you place it too early on in the code, an error will occur because one of the values doesn't exist yet. Remember, the code executes based on order, so you cant have a statement run any variable that is not declared first.