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 trial

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,558 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.