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 trialClassified Classified
767 PointsWhat is an illegal start of type?
String noun;
do {
noun = console.readLine("Enter a noun: ");
boolean isInvalidWord = (noun.equalsIgnoreCase("dork") ||
noun.equalsIgnoreCase("jerk") ||
noun.equalsIgnoreCase("wolf"));
if (isInvalidWord) {
console.printf("That language is not allowed. Try again. \n\n");
}
} while(isInvalidWord);
Picked up JAVA_TOOL_OPTIONS: -Xmx128m
Picked up _JAVA_OPTIONS: -Xmx128m
TreeStory.java:36: error: cannot find symbol
} while(isInvalidWord);
^
symbol: variable isInvalidWord
location: class TreeStory
TreeStory.java:36: error: illegal start of type
} while(isInvalidWord);
^
2 errors
1 Answer
andren
28,558 PointsThe problem with your code is that the isInvalidWord
variable is declared within the do/while
loop. Due to the way variable scoping works a variable declared within a loop is not accessible outside of the loop, and the while
statement is considered to be outside the scope of the loop.
Therefore when you reference isInvalidWord
as the condition of your loop Java doesn't know what it is. If you move the declaration outside the loop like this:
String noun;
boolean isInvalidWord; // Declare isInvalidWord
do {
noun = console.readLine("Enter a noun: ");
isInvalidWord = (noun.equalsIgnoreCase("dork") || // Changed from declaration to simply assigning a value
noun.equalsIgnoreCase("jerk") ||
noun.equalsIgnoreCase("wolf"));
if (isInvalidWord) {
console.printf("That language is not allowed. Try again. \n\n");
}
} while(isInvalidWord);
Then your code should work fine.