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
Mike Papamichail
4,883 PointsImproving The TreeStory program!
Hello there, i finished the first part of the java track and i read the teacher notes which gave us a few hints on how to improve the program.However, i decided not to follow them exactly and use an array to store the "bad words" instead of using the contains() method.However, inside my do..while loop, the while loop inside doesn't run making my program loop forever.Any ideas on why that happes?I've tried a TON of things but nothing worked for me.Maybe i am lacking some logic.Thanks in advance.
CODE :
import java.io.Console;
public class TreeStory {
public static void main(String[] args) {
Console console = System.console();
/* Some terms:
noun - Person, place or thing
verb - An action
adjective - A description used to modify or describe a noun
Enter your amazing code here!
*/
// _name_ is a _adjective_ _noun_.They are always _adverb_ _verb_.
String ageAsString = console.readLine("How old are you?\n");
int age = Integer.parseInt(ageAsString);
if (age < 13) {
console.printf("You must be at least 13 to run this program.\n");
System.exit(0);
}
String name = console.readLine("Enter your name:\n");
String adjective = console.readLine("Enter an adjective:\n");
// Stopping users from using inappropriate language
String[] noun = {"dork","jerk","nerd"};
String noun1;
boolean isInvalidWord = true;
// declaring this variables outside of the codeblock below, allows the variables to have a global scope
do {
noun1 = console.readLine("Enter a noun:\n"); /* if i were to declare this variable inside this codeblock, then it would have a local scope and so, it wouldn't be accessible anywhere else -inside the program- outside of this codeblock */
int i = 0;
while ( noun1 == noun[i] && i < 2 ) {
isInvalidWord = false;
i = 3;
}
if (isInvalidWord) {
console.printf("That kind of language is not allowed.Retry\n");
}
} while(isInvalidWord); // for example(if isInvalidWord had a local scope),this would throw an error
String adverb = console.readLine("Enter an adverb:\n");
String verb = console.readLine("Enter a verb(ending in -ing):\n");
console.printf("Your TreeStory:\n----------\n");
console.printf("%s is a %s %s.He/she is always %s %s.\n----------\n",name,adjective,noun,adverb,verb);
}
}