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 trialNicholas Demetriou
639 PointsStuck at Stage 3 Challenge Task 1 of 2
for some reason i'm stuck, i can't get around this one.
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
3 Answers
Stone Preston
42,016 Pointstask 1 states: Add an if statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to second".
to test if one string equals another, you use the equals method.
the documentation on the String class (specifically the equals method) states:
public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
so the equals method returns true if the String you call the method on has the same sequence as characters as the String object you pass in to it
myString.equals(someOtherString) // returns true if myString and someOtherString have the same character sequence
so to test if firstExample is equal to the secondExample you would use:
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
Nicholas Demetriou
639 Pointsthis is what i was doing, the only difference is that i was placing it in the between the 2nd and 3rd line!
Stone Preston
42,016 Pointsi tried that and passed task 1 using:
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
String thirdExample = "HELLO";
Nicholas Demetriou
639 Pointsi will look into it
cheers Stone