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 trialKlaudia Krol
576 Pointsif statement that checks to see if firstExample is equal to secondExample. If it is, print out "first is equal to help?
Hi Dear Treehouse friends ,
Could you please have look whats rong with my code ?
Thanks , Klaudia
7 Answers
Jennifer Nordell
Treehouse TeacherThat's very strange. Because if I paste in this code in the challenge, it passes both steps. Keep in mind that I have not altered the segment of code that I posted previously.
// 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");
}
if (firstExample.equalsIgnoreCase(thirdExample)) {
console.printf("first and third are the same ignoring case");
}
First, make sure you understand this code and what it does. Then copy/paste this into the challenge (to make sure all the spelling is exactly the same and no stray punctuation marks). If it still doesn't pass, then you may need to contact support at the link at the top of this page. Good luck!
Jennifer Nordell
Treehouse TeacherHi there Klaudia Krol! Santos Solorzano is correct. You need to be using the .equals()
method to compare the strings. You've asked us to point out what's incorrect in your code, however, your code isn't posted here. So I'm going to post the section you need for the first step and then walk you through it.
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
Here we set up an if statement to see if firstExample
is equal to secondExample
. As mentioned previously, we use the .equals()
method to accomplish this. If the result is true, then we use console.printf
to print out the string as specified by the challenge instructions. Hope this helps!
Klaudia Krol
576 PointsHi Jennifer ,
It's not working .
https://teamtreehouse.com/library/java-basics/perfecting-the-prototype/string-equality
Klaudia Krol
576 PointsThank you all for help :-)
Best code practise
Ikuko Schoeller
6,116 PointsI know it is too late too say. However I had a same problem. I could not find any mistakes. I closed the browser then started over again. Then the code was accepted. Whenever something wired happen, I usually close the browser or clear the cash. Sometimes it solve the problem!
Diana Ci
18,672 Pointsmy problem with this code challenge was that I added System.exit(0); and the code doesn't work with it :)
Fredrick Reid
7,354 PointsIdk if this helps but this is the code i used and it passed the first and second task Just make sure you use the right string method to ignore the case on the second task
``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"); }
Santos Solorzano
3,125 PointsSantos Solorzano
3,125 PointsHi Klaudia,
Are you using the equals() method to see if firstExample is equal to secondExample?