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
Unsubscribed User
427 PointsMy code makes five times the same action but one of this five times the code does not work.
I'm currently writing a slightly more advanced code of "TreeStory" to practice the concepts I learned. But while this part of my code works perfectly :
do {
name = console.readLine("\nEnter a name : ");
invalidWordName = (name.equalsIgnoreCase("John") || name.equalsIgnoreCase("Smith"));
if (invalidWordName) {
console.printf("That name is not allowed ! Try again...\n");
}
} while (invalidWordName);
This one does not work, it does not censor the desired words:
do {
adjective = console.readLine("\nEnter an adjective : ");
invalidWordAdjective = (name.equalsIgnoreCase("smart") || name.equalsIgnoreCase("incredible"));
if (invalidWordAdjective) {
console.printf("That adjective is not allowed ! Try again...\n");
}
} while (invalidWordAdjective);
Here is my full code : http://pastebin.com/u2LwfdX9.
How can I fix this ?
2 Answers
Jennifer Nordell
Treehouse TeacherThe problem, in my opinion, is that you're looking at the wrong variable. Take a look:
do {
adjective = console.readLine("\nEnter an adjective : ");
invalidWordAdjective = (name.equalsIgnoreCase("smart") || name.equalsIgnoreCase("incredible"));
if (invalidWordAdjective) {
console.printf("That adjective is not allowed ! Try again...\n");
}
} while (invalidWordAdjective);
I feel like your definition of invalidWordAdjective should be based on the variable adjective... not name. Try this instead:
invalidWordAdjective = (adjective.equalsIgnoreCase("smart") || adjective.equalsIgnoreCase("incredible"));
Unsubscribed User
427 PointsOf course... I'd be more careful next time. Thank you for finding my mistake !
Jennifer Nordell
Treehouse TeacherYou're quite welcome! It happens to us all. Kudos by the way on experimenting on your own. The absolute best way to learn to code is to break something and then fix it :)
Jeremy Hill
29,567 PointsAlso in the while loop you could it the same way:
do{
//run this code
}while(name.equalsIgnoreCase(invalidWordName))
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsYou could do: