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

Issue w Java

I have a 'do while' loop and for the loop to end the 'isInvalidWord' has to equal false.

I am checking if a curse word is in a string, and I was wondering how to also use 'equalsIgnoreCase' while checking if the string has an invalid word.

I have no idea how to put 'isInvalidWord = string.contains(curseWord));' and 'isInvalidWord = string.equalsIgnoreCase(curseWord));' in the same variable.. I have tried '&&', but I am not very good with all this stuff.

Sorry for the confusion.

Martin Gallauner
Martin Gallauner
10,808 Points

Hi Zach,

posting your code would be helpful in gerneral. I think you could convert your string to lowercase (.toLowerCase() )and then do string.contains(curseWord).

do
        {
            //Getting insult
            System.out.println("Throw your best insult at me, bet you can't insult me.\n");
            String insult = s.nextLine();

            //Checking if insult has a curse word
            isInvalidWord = (insult.contains(curseWord));


            //Filtering Words
            if (isInvalidWord)
            {
                System.out.println("Try that insult again, just without cursing.\n");
            }

            //Looping if insult does have a curse word(s)
        }while (isInvalidWord);
        //Output once conditions are met
        System.out.println("Oucchh! That was a good one.\n");

1 Answer

I think you can try it this way?

with method chaining, you can bring user reply to a lowercase and make sure curseWord is lowercase.

// original code
isInvalidWord = (insult.contains(curseWord));

// bring insult to lowercase not having to deal with case sensative anymore
isInvalidWord = (insult.toLowerCase().contains(curseWord)); 

// or this if you haven't put curseWord all to lowercase. 
isInvalidWord = (insult.toLowerCase().contains(curseWord.toLowerCase()));