Java Java Basics Perfecting the Prototype Looping until the value passes

Help with error code: "Boolean cannot be dereferenced"

So i'm new to this and during my challenge i keep getting this error message but i can't figure out how to fix it, or rather what i'm doing wrong i suppose. I thought i did it right, can anyone explain to me what i'm doing wrong? Thank you! ~Ryan

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String question;
boolean response;
do {
  question = console.readLine("Do you understand do while loops?  ");
  response = (question.equalsIgnoreCase("yes"));
  if (response) {
    console.printf("Congrats");
  }
} while(response);

1 Answer

Rob Bridges
Rob Bridges
Java Web Development Techdegree Student 13,600 Points

Hey there Rian,

The trouble that is getting caused is because you're trying to set up your boolean, I think that java thinks you're trying to change the value to be the string representation of true, instead of a boolean.

In reality you have all the code that you need to pass this challenge minus a few adjustments (good job). The first thing to do is take out the boolean.

Also, be sure to name your string as response, otherwise the challenge won't allow you to move on to the next step, so it should look something like, and when we're checking to see if we want to break out of the loop, check to see if the response is equal to no. Other than that, good job again!

String response;

do {
 response = console.readLine("Do you understand do while loops?  ");


} while(response.equalsIgnoreCase("no"));

Thanks I hope this helps, feel free to shout at me if it doesn't.

Ooohhh okay, the course just prior to the quiz was talking about booleans in the end, so when the question talked about storing the answer i assumed thats what it was asking me to do, lol. i'm still a little unclear about how "do" and "while" works, but hopefully it'll be discussed more in the next courses. Thank you very much for your help, i really appreciate it :) ~Ryan

Rob Bridges
Rob Bridges
Java Web Development Techdegree Student 13,600 Points

Hey Rian, no worries, this was actually a really well done, it looks like you trying to overcomplicate it a bit. Happens to the best of us.

A do while loop is more or less like this do

do {

//the code that you want to be repeated until a certain condition is met

} while (//condition that if met continues the loop)

The while part of it is always a little sketchy at first, but think of it this way, in a do while loop, as long as that condition is met, the code will continue to execute. and the only way to break out of the loop is to make that condition be false.

Thanks, I hope that helps.

yea actually that helps alot, i wasn't sure exactly how it worked at first but now i see :) I made it through the second part of the quiz as well, of course now i'm a little stumped on the final question asking,"using console.printf print out a formatted string that says "Because you said <response>, you passed the test!"". I'm a little unsure how to do this exactly, so do i create a new variable called answer or something and put it just below the "do" "while" section? i'm just not sure how to add a second answer into this string that would create a different printed answer :/ sorry to bother you.

Rob Bridges
Rob Bridges
Java Web Development Techdegree Student 13,600 Points

Hey Rian, No worries.

So what the question is wanting is for you to print a formatted string and pass the variable in directly.

a formatted string basically lets us use place holders an example of one would be,

console.printf("Hi, my name is %s", name);

What this does is because %s is the place holder it won't actually print that we're telling java to pass in the string name in that place.

So assuming we had something like

String firstName = "Rob"
favoriteColor = "blue";

console.printf ("Hi, my name is %s, and my favorite color is %s", firstName, blue);

What we're doing here is about the same, except that we're telling java to pass in firstName as the first value, and blue for the second, as it will always follow a linear path.

So this program is expecting you to write. is that line but where they put the the variable name in brackets they want you use %s and pass in the value of our response String. if you just copy the above syntax, just change the variables and string to copy what they're expecting you'll pass it.

Don't hesitate to shoot back if this is confusing, and I'll try and clarify.

Thanks!

Gaawwwwdddd are you serious?! I remember doing that in like the second course! wow, i was waaaaayyyy overthinking that, i thought i had to somehow add another "if" statement for in case they answered yes instead of no and printf what the response to that answer would be. wow, epic facepalm moment, lol. I think what i'll do is just go back and watch all of the videos up til this point to refresh my memory and help drill it all into my head. Thanks again for all your help man, you're a savior :)

Rob Bridges
Rob Bridges
Java Web Development Techdegree Student 13,600 Points

Hey Rian,

It's okay, I'm terrible at overthinking things too! unfortuantely the better you get at java and the more tools you have the harder you can make things for yourself.

It definately gets easier with time!

Good luck with everything, don't hesitate to reach back out to the forums or me if you have any more questions.