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

My 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 ?

You could do:

 if(name.equalsIgnoreCase(invalidWordName)) {
  //run this code
}
else{
 // run this code instead
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

The 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"));

Of course... I'd be more careful next time. Thank you for finding my mistake !

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

You'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 :)

Also in the while loop you could it the same way:

do{
   //run this code
}while(name.equalsIgnoreCase(invalidWordName))