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 Java Basics Perfecting the Prototype Looping until the value passes

janeporter
PLUS
janeporter
Courses Plus Student 23,471 Points

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

Example.java
// 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
Christopher Augg
21,223 Points

Hello 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

janeporter
janeporter
Courses Plus Student 23,471 Points

Thanks. I figured that out right after I posted this.

The 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.

button to click compiler error

edited 09/04/2015 17:02: Slight correction thanks Chris.

Christopher Augg
Christopher Augg
21,223 Points

Nice 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

Python on the brain (so I've got white-space sensitivity) ... thanks for the information, learned something new. Thanks Chris!