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 trialRonnie Barua
17,665 PointsAdd an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to
Thanks for your help. Here is what I have:
if (firstExample.equals("secondExample")) { console.printf("first is equal to second"); }
17 Answers
Juan Gallardo
1,568 PointsYou added quotes around the variable. This is the answer
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
Craig Dennis
Treehouse TeacherIt looks like you might've quoted a variable accidentally....
Ronnie Barua
17,665 PointsThanks for your quick response. No there weren't any accident. It just won't work.
Craig Dennis
Treehouse Teacher"secondExample" // Doesn't need the quotes ;)
Kevin Atkins
8,965 PointsI had the same problem. You are supposed to put the if statement at the very bottom of the variables.
Zach Forrest
3,228 PointsIt worked thanks Kevin! Ended up with this..
String firstExample = "hello"; String secondExample = "hello"; String thirdExample = "HELLO"; if (firstExample.equals(secondExample)) { console.printf("first is equal to second"); }
David Franco
3,252 PointsKevin's answer worked for me too!
Joe Goodall
3,483 PointsJust in case it helps, I tried this answer 1st...
if (firstExample == secondExample); { console.printf("first is equal to second");
System.exit(0); }
It didn't work because, as explained on stackoverflow:-
== tests for reference equality.
.equals() tests for value equality.
Consequently, if you actually want to test whether two strings have the same value you should use .equals().
http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java
Hope this helps anyone who made the same mistake.
harmenklamer
Courses Plus Student 206 PointsHere's what I have:
// 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");
}
But if I understand this correctly, it should display "first is equal to second". My screen just stays empty. It does not show any errors. So I guess the code is 'correct', but I am still doing something wrong.
Oh well, time to go to bed for now.
Mauricio Prieto
1,055 Pointsyou need the System.exit(0) for it to display
Gloria Dwomoh
13,116 PointssecondExample is a variable the same way as firstExample is, hence you don't need to add quotation marks to it :)
Ronnie Barua
17,665 PointsThanks Gloria! I'll try just that.
Ronnie
Gloria Dwomoh
13,116 PointsYou are welcome.
Kristie Campbell
2,120 PointsCan anyone explain why the "if" statement goes below all of the strings? In the example of using the noun.equals("dork") scenario it was added right after the noun string... that's why I put my "if" statement where I did. Help me understand the logic! :)
Jeffrey Beauchamps
8,437 PointsComputer language reads code line by line. So for the if statement to work properly, the firstExample and secondExample strings needs to be read before the if statement can see if both are equal or not.
Now with the noun.equals("dork") scenario. The if statement is looking for the noun string, which is written before the if statement.
Conclusion: If statement only can read what's written before it and doesn't read nothing after it.
//Here the if statement only can read firstExample
String firstExample = "hello";
if (firstExample.equals(secondExample)){
console.printf("first is equal to second");
String secondExample = "hello";
String thirdExample = "HELLO";
}
// Here the if statement now can read firstExample and secondExample
String firstExample = "hello";
String secondExample = "hello";
if (firstExample.equals(secondExample)){
console.printf("first is equal to second");
String thirdExample = "HELLO";
}
```
bogdan777
1,908 PointsJoe i have tried your solution with == as well and didnt worked
Learning coding
Front End Web Development Techdegree Student 9,937 PointsHi all,
I thought about posting this question in a separate post, but it seemed appropriate here. My question is why i get an error when I want to compile this code?
import java.io.Console;
public class Equality {
public static void main(String[] args) {
Console console = System.console();
// 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"); }
This is the output of the console;
treehouse:~/workspace$ javac Equality.java
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
Equality.java:13: error: reached end of file while parsing
}
^
1 error
treehouse:~/workspace$
What am I doing wrong and why? Thanks.
Craig Dennis
Treehouse TeacherDon't add the class bits just yet. Assume you are in the main method and the console object exists.
Code looks good.
Learning coding
Front End Web Development Techdegree Student 9,937 PointsThanks for the answer, but I didn't understand what you meant with class and bits. (or wich class bits)
fazal pasha
216 PointsBummer! Try again! what is errors
Arturs Certuks
13,059 PointsString firstExample = "hello";
if (firstExample.equals ("hello")) {
console.printf("first is equal to second");
}
String secondExample = "hello";
String thirdExample = "HELLO";
..THIS IS THE ONLY THING THAT WORKED FOR ME!!!
Stephen Behinds
Courses Plus Student 308 PointsI passed by this : )
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equals(secondExample)){
console.printf("first is equal to second"); }
if(firstExample.equalsIgnoreCase(thirdExample)){
console.printf("first and third are the same ignoring case"); }
Isha Dhital
1,376 PointsAll you have to do is delete the System.exit(0); and it should work!
Muhammad Asif
Courses Plus Student 557 Points// 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"); }
Amit Murde
718 Pointsneed to remove System.exit(0)
Augusto Hernandes
1,818 PointsHi, guys! This is the answer:
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second"); }
terrimodomo
6,153 PointsAwesome...it worked, Thank you!
Annabelle Lewis
306 PointsAnnabelle Lewis
306 PointsIt did not accept your answer, your answer is incorrect.
Awais jamil
Java Web Development Techdegree Student 4,742 PointsAwais jamil
Java Web Development Techdegree Student 4,742 PointsIncorrect answer
Alex Davis
35,050 PointsAlex Davis
35,050 PointsThen both of you put the wrong thing. Check your code for errors.
Michael Mabin
2,788 PointsMichael Mabin
2,788 PointsIt worked. Thank you!