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 trialTom Pattison
470 PointsNeed some help
I'm trying to complete the extra credit for the java basics and I've followed the process for creating the do while loop. I think I must be missing something because whenever I run the program, it doesn't prompt the user to try again after the blacklisted words are entered.
Also, is there a way I can get the contains method to ignore case?
Any help is greatly appreciated! Here is my code:
String firstName = console.readLine("Enter your first name: ");
String lastName = console.readLine("Enter your last name: ");
String adjective = console.readLine("Enter an adjective: ");
String noun;
boolean isInvalidWord;
do {
noun = console.readLine("Enter a noun: ");
isInvalidWord = (noun.contains("Jerk, dork, nerd, geek"));
if (isInvalidWord) {
console.printf("Sorry that language isn't allowed. Try again.\n\n");
}
} while(isInvalidWord);
1 Answer
Steven Parker
231,271 PointsThe "contains" argument must be present in entirety to match, so this example would only match if all 4 words were present in that particular order separated by commas. You'll need to match each word separately, or store them in an array and use a loop to test each one.
And you can make your comparison case-insensitive by converting the case before testing:
isInvalidWord = noun.toLowerCase().contains("jerk");
isInvalidWord |= noun.toLowerCase().contains("dork");
// etc.
Tom Pattison
470 PointsTom Pattison
470 PointsThanks! That worked :)
Can I ask what the purpose of the open close parentheses after "toLowerCase" is?
Steven Parker
231,271 PointsSteven Parker
231,271 PointsAnytime you call a method, you place parentheses after the method name whether or not you pass any arguments within them.
Tom Pattison
470 PointsTom Pattison
470 PointsThanks a lot for your help!