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

Ronnie Barua
Ronnie Barua
17,665 Points

Add 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

You added quotes around the variable. This is the answer

if (firstExample.equals(secondExample)) { 
  console.printf("first is equal to second"); 
}
Annabelle Lewis
Annabelle Lewis
306 Points

It did not accept your answer, your answer is incorrect.

Incorrect answer

Then both of you put the wrong thing. Check your code for errors.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

It looks like you might've quoted a variable accidentally....

Ronnie Barua
Ronnie Barua
17,665 Points

Thanks for your quick response. No there weren't any accident. It just won't work.

Craig Dennis
Craig Dennis
Treehouse Teacher
"secondExample"   // Doesn't need the quotes ;)
Kevin Atkins
Kevin Atkins
8,965 Points

I had the same problem. You are supposed to put the if statement at the very bottom of the variables.

Zach Forrest
Zach Forrest
3,228 Points

It 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
David Franco
3,252 Points

Kevin's answer worked for me too!

Joe Goodall
Joe Goodall
3,483 Points

Just 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.

Here'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
Mauricio Prieto
1,055 Points

you need the System.exit(0) for it to display

secondExample is a variable the same way as firstExample is, hence you don't need to add quotation marks to it :)

Ronnie Barua
Ronnie Barua
17,665 Points

Thanks Gloria! I'll try just that.

Ronnie

You are welcome.

Kristie Campbell
Kristie Campbell
2,120 Points

Can 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! :)

Computer 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";

}



          ```

Joe i have tried your solution with == as well and didnt worked

Learning coding
seal-mask
.a{fill-rule:evenodd;}techdegree
Learning coding
Front End Web Development Techdegree Student 9,937 Points

Hi 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
Craig Dennis
Treehouse Teacher

Don't add the class bits just yet. Assume you are in the main method and the console object exists.

Code looks good.

Bummer! Try again! what is errors

String 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
PLUS
Stephen Behinds
Courses Plus Student 308 Points

I 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
Isha Dhital
1,376 Points

All you have to do is delete the System.exit(0); and it should work!

Muhammad Asif
PLUS
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
Amit Murde
718 Points

need to remove System.exit(0)

Hi, 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"); }

Awesome...it worked, Thank you!