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 trialBen Taylor
354 Pointsi dont understand how to fix this if (firstExample == thirdExample.equalsIgnoreCase()) { printf("first and ....}
i dont understand how to fox this to check whether the first ad third statement are the same regardless of case
// I have imported a java.io.Console for you, it is named console.
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample == secondExample) {
console.printf("first is equal to second");
}
if (firstExample == thirdExample.equalsIgnoreCase()) {
printf("first and third are the same ignoring case");
}
1 Answer
Andrei Fecioru
15,059 PointsHi,
I would do something like this:
if (secondExample.equals(firstExample)) {
console.printf("first is equal to second");
}
if (thirdExample.equalsIgnoreCase(firstExample)) {
console.printf("first and third are the same ignoring case");
}
When you do string equality tests between two strings (let's say str1
and str2
) you don't do str1 == str2
; you should call the equals()
method on one of the strings and pass the other string as input argument, like str1.equals(str2)
.
Hope this helps.
Ben Taylor
354 PointsBen Taylor
354 Pointsthankyou, this was very helpful
Travis Christian
19,878 PointsTravis Christian
19,878 PointsAndrei's solution is almost perfect, but you must include the "console" before "printf", like so:
if (secondExample.equals(firstExample)) { console.printf("first is equal to second"); } if (thirdExample.equalsIgnoreCase(firstExample)) { console.printf("first and third are the same ignoring case"); }
Andrei Fecioru
15,059 PointsAndrei Fecioru
15,059 PointsThanks for spotting that. Fixed it.