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 trialfoxtails
3,655 PointsfirstExample equal to thirdExample. I'm stuck.
I can't find the error. First statement worked. This doesn't.
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equalsIgnoreCase("HELLO")) {
console.printf("first and third are the same ignoring case");
}
foxtails
3,655 PointsStill don't see it. And the semi colon is not needed in Workspace code that worked. Tried it anyway didn't work.
4 Answers
Steve Hunter
57,712 PointsHi Foxtails ...
You need to compare the two variable contents, not a string 'literal' like "HELLO
".
So, rather than comparing firstExample
to "HELLO
", compare it (ignoring case) to thirdExample
. That is the variable that contains "HELLO
".
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 is equal to third");
}
I hope that helps.
Steve.
Steve Hunter
57,712 PointsHi there,
You do need to leave all the previous code intact, so your end result should have at least 9 lines of code, like my answer did.
What error are you getting?
Steve.
foxtails
3,655 PointsI have corrected the problem you both pointed out, but I still am stuck. It won't accept it. Here is my coding:
if(firstExample.equalsIgnoreCase(thirdExample)){
console.printf("first is equal to third");
}
foxtails
3,655 PointsHi Steve, you just DID IT! That was it. I did not leave the previous example, but modified it to fit the challenge for the next task. And that caused the problem. Thank you so much!
Steve Hunter
57,712 PointsExcellent! Glad you got it fixed.
You will rarely need to delete code from earlier tasks; best just leave it there unless the challenge says specifically not to.
Dmytro Konkov
533 PointsDmytro Konkov
533 Points[MOD: edited code block & added code comments - see answer below]