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

Geovani Estacio
PLUS
Geovani Estacio
Courses Plus Student 1,875 Points

Still stuck on this one and can't seem to get passed it, please help!

String firstExample = ("hello"); if (firstExample.equals("secondExample") ) { console.printf("first is equal to second"); System.exit(0);

} String secondExample = "hello";

String thirdExample = "HELLO

Equality.java
// I have imported a java.io.Console for you, it is named console. 
String firstExample = ("hello");
if (firstExample.equals("secondExample") ) {
   console.printf("first is equal to second");
   System.exit(0);

}
String secondExample = "hello";

  String thirdExample = "HELLO";

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you're comparing the variable firstexample to the string secondexample, not the variable secondexample. so it says hello is not equal to secondexample, instead of hello is equal to hello. remove the quotes around secondexample and try that.

I cannot remember the exact challenge you are on, but it looks to me like you need to move the second and third example String declarations to the top, above the if statement. If a variable is not initialized before, the if statement does not know what it is you are referring to and wanting to compare (or use in general) since the code is read from the top down.

Also, you do not need the parenthesis around the "hello" in the firstExample variable declaration. Using the quotes signifies to the interpreter that the value is a String value.

And a third note... In the condition of the if statement, where you are telling it what to compare to what, you need to remove the quotation marks from inside the .equals(). By using the quote marks, you are again just telling the interpreter to check to see if the value of a String called firstExample is equal to a value of "secondExample"... literally. What I believe you are attempting to do is to test to see if the value of firstExample is equal to the value of secondExample. To do this, all you need to do is put the variable name that you are wanting to test against inside the parenthesis of the .equals()

Assuming both String variable values had the same spelling and capitalization, you would get the same result whether you used the quotes or not, but I'm sure the task is trying to check equality between two values, and this is why you need to make sure to use the variable name instead of a string with the quotes in the .equals.

String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";

if (firstExample.equals(secondExample) ) {
   console.printf("first is equal to second");
   System.exit(0);

}