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 trialSean Keegan
6,255 PointsCan I get a little help my friends?
I followed along with the video, and skipped ahead guessing what I would have to do next. I got a couple of small things wrong, and so I corrected them. When ran the code, it did not work, and I got some errors (2). I thought that they would be easy enough to fix, and so I copied the exact code in the video.
For some reason, it doesn't seem to work for me. I'm pretty sure I've made a silly mistake somewhere, but I can't seem to find it. I'll post my code, and if anyone could point out what is wrong, and how to fix it, I would be most grateful.
String adjective = console.readLine("Enter an adjective: ");
String noun;
do {
noun = console.readLine("Enter a noun: ");
boolean isInvalidWord = (noun.equalsIgnoreCase("dork") ||
noun.equalsIgnoreCase("jerk") ||
noun.equalsIgnoreCase("nerd"));
if (isInvalidWord) {
console.printf("That langauge is not allowed. Try again. \n\n");
}
} while (isInvalidWord);
String verb = console.readLine("Enter a verb: ");
String adverb = console.readLine("Enter an adverb ending in -ing: ");
Apologies for such a lengthy read! Thanks in advance!
3 Answers
Rebecca Rich
Courses Plus Student 8,592 PointsYou are running into a scoping error.
do {
noun = console.readLine("Enter a noun: ");
boolean isInvalidWord = (noun.equalsIgnoreCase("dork") ||
noun.equalsIgnoreCase("jerk") ||
noun.equalsIgnoreCase("nerd"));
if (isInvalidWord) {
console.printf("That langauge is not allowed. Try again. \n\n");
}
} while (isInvalidWord);
isInvalidWorld is not defined in the scope of the do-while statement. It is only defined within the two curly brackets of the do block. To fix this define the boolean isInvalidWord before the do-while statement:
boolean isInvalidWord;
do {
noun = console.readLine("Enter a noun: ");
isInvalidWord = (noun.equalsIgnoreCase("dork") ||
noun.equalsIgnoreCase("jerk") ||
noun.equalsIgnoreCase("nerd"));
if (isInvalidWord) {
console.printf("That langauge is not allowed. Try again. \n\n");
}
} while (isInvalidWord);
Florin Veja
6,912 PointsWhat are the errors you are receiving?
Sean Keegan
6,255 PointsI should have included this first time round, my bad.
TreeStory.java:30: error: cannot find symbol
} while (isInvalidWord);
^
symbol: variable isInvalidWord
location: class TreeStory
TreeStory.java:30: error: illegal start of type
} while (isInvalidWord);
^
2 errors
Sean Keegan
6,255 PointsSean Keegan
6,255 PointsThank you!