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 trialjaneporter
Courses Plus Student 23,471 Pointscode not working in code challange, but similar code works in workspaces...
This code isn't working in the Java Basics code challange
String response;
do {
response = console.readLine("Do you understand do while loops? ");
} while (response("No"));
But similar code:
String noun;
boolean isInvalidWord;
do {
noun = console.readLine("Enter a noun: ");
isInvalidWord = (noun.equalsIgnoreCase("dork") ||
noun.equalsIgnoreCase("jerk"));
if (isInvalidWord) {
console.printf("That language is not allowed. Try again. \n\n");
}
} while (isInvalidWord);
works in Workspaces... I'm defining the string 'response outside of the loop. Why is this still not working?????
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do {
response = console.readLine("Do you understand do while loops? ");
} while (response("No"));
2 Answers
Christopher Augg
21,223 PointsHello Jane,
The only issue with your code is not using the response.equalsIgnoreCase("No") method. You are using:
while (response("No"));
When you need:
while (response.equalsIgnoreCase("No"));
Remember, response is not a method, it is a String object. String objects have methods that we can call like equals() and equalsIgnoreCase(). You can see all of the String's methods by looking at its documentation .
I hope this helps.
Regards,
Chris
Carl Hicks
Java Web Development Techdegree Student 3,453 PointsThe first thing I see at a quick glance is that you have a space between after while, which will case a syntax error.
// You want to define you variables outside the loop
String response;
boolean isResponseInvalid;
do {
// this is the point in which you are assigning things to the variables
response = console.readLine("Do you understand do while loops?");
isResponseInvalid = response.equalsIgnoreCase("no");
// This is where you are checking to see if your condition is true or not
if (isResponseInvalid){
console.printf("Please retry ... or something of that nature.");
}
} while(isResponseInvalid);
So clean you the syntactical error and [Y]ou have a little scope issue. If you are still having issues, I'd be happy to explain in more detail. Also just as a hint when getting errors on code challenges, if you click on the preview button you can see the compiler errors which are super helpful in pointing you to what is going wrong in your code.
edited 09/04/2015 17:02: Slight correction thanks Chris.
Christopher Augg
21,223 PointsNice explanation Carl. However, I would like to clear up something about spaces in Java.
- Spaces in Java do not matter. While it is not a good practice, having spaces after the while statement before the () would not cause a syntax error. For example, the following code will compile/pass:
String response;
do {
response = console.readLine("Do you understand do while loops? ");
} while (response.equalsIgnoreCase("No"));
Regards,
Chris
Carl Hicks
Java Web Development Techdegree Student 3,453 PointsPython on the brain (so I've got white-space sensitivity) ... thanks for the information, learned something new. Thanks Chris!
janeporter
Courses Plus Student 23,471 Pointsjaneporter
Courses Plus Student 23,471 PointsThanks. I figured that out right after I posted this.