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 trialmartin li
1,026 PointsQuestion about the method calling
Yeah so I completed the assignment by using the 'not equal' operator and I feel like I have completed the assignment but didn't complete it by using the equalsIgnoreCase method and I want to know how I would complete the assignment doing that (see my attempt in the comments)
(Add another if statement that checks if the firstExample is equal ignoring case to thirdExample. If it, is print out "first and third are the same ignoring 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) {
console.printf("first and second are the same ignoring case");
}
/*
if (firstExample == thirdExample.equalsIgnoreCase("HELLO")){
console.printf("first and third are the same ignoring case");
}*/
1 Answer
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHey there Martin,
So there are two methods that Craig wanted us to call on these Strings.
The first one is equals() which is predefined in the String class and can be passed on any String object.
We use this one in the first test like so
if (firstExample.equals(secondExample)
This will check and see if they exactly equal.
If we wanted to get a little Picky, say we wanted the second Example to be "Hello" and we wanted to see how close it was to the first, we would pass
if(firstExample.equalsIgnoreCase(secondExample))
This will check and if each character is the same (capitalization not matterring) it will return true.
The equalsIgnoreCase() method is actually used pretty frequently on strings, especially when acceping input from the user. We don't want to force them type "Yes" when "yes" works just as well.
In fact you'll be using the equalsIgnoreCase() method later in this course.
Over all, I changed your code to
String firstExample = "hello";
String secondExample = "hello";
String thirdExample = "HELLO";
if (firstExample.equals(secondExample)) {
console.printf("first is equal to second");
}
if (firstExample.equalsIgnoreCase(secondExample)) {
console.printf("first and second are the same ignoring case");
}
Thanks, let me know if this doesn't help.