Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Razvan Cirstea
Razvan Cirstea
2,493 Points

What is the syntax difference between equalsIgnoreCase and toLowerCase ?

Hi,

I hope my following question isn't too silly. I've just started learning Java and I have been trying to solve step 2 of the Extra Credits. I've managed to solve it, however I only did it by using the following code in the loop :

boolean isInvalidWord; String words=" dork nerd "; do { noun=console.readLine("Enter a noun "); isInvalidWord=words.contains(noun.toLowerCase()); if (isInvalidWord) { console.printf("Bad language is not allowed. Try again. \n\n"); } } while (isInvalidWord);

I have been trying to solve step 2 also by using the equalsIgnoreCase object instead of toLowerCase, ,but without any success. This is one of the many solutions I tried for equalsIgnoreCase :

isInvalidWord=words.contains(noun.equalsIgnoreCase());

Am I failing because equalsIgnoreCase is not supposed to be used together with contains ? as it is only used to compare a variable with a specific string ?

Many thanks, Razvan

1 Answer

andren
andren
28,526 Points

toLowerCase is a method that simply returns a lowercase version of the thing you call it on. equalsIgnoreCase is a method that compares the thing you call it on to the thing you pass into it and returns a boolean (true or false) based on whether or not they are equal.

So they do indeed do very different things. Since contains expects you to pass it a string it will not work with a method which does not return one. Also the equalsIgnoreCase requires that you pass it something to compare the string to, it won't work without that.

Java like most languages is pretty dynamic in terms of allowing you to mix various methods together, but in order for them to work together they need to produce the value that the other method expects you to pass it.

Razvan Cirstea
Razvan Cirstea
2,493 Points

thank you very much for your answer, Andren, I understand the ideea behind the two methods now.