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 trialJonny Patterson
7,099 PointsUsing toLowerCase()
In the extra credit exercise I could only get toLowerCase() to work by doing the following:
String noun;
String badWord = "nerd";
boolean isInvalidWord;
do {
noun = console.readLine("Enter a noun: ");
String lower = noun.toLowerCase();
isInvalidWord = lower.contains(badWord);
if (isInvalidWord) {
console.printf("That language is not allowed. Try again.");
}
} while(isInvalidWord);
Whilst this appears to work correctly, I am concerned it is verbose. Is there a better way to do this?
Regarding the "bad word" list, this only appears to work with more than one word if 'noun' matches what is stored in the 'badWord' string exactly. Would the correct way to create a bad word list be to use an array?
1 Answer
Allan Clark
10,810 PointsIn Java just as in pure mathematics there are lots of ways to do things and not one "correct" way. I answered this problem this way:
noun = console.readLine("Enter a noun: ");
noun = noun.toLowerCase();
while(badWord.contains(noun)) {
noun = console.readLine("Hey thems fighting words try again: ");
}
Jonny Patterson
7,099 PointsJonny Patterson
7,099 PointsThank you for taking the time to reply and sharing your solution. Helpful to see other approaches!