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

Variable might not have been initialized Task 2/3

My code looks like it works, but when I click 'preview' it says code might not have been initialized in the boolean, why is this so and how can I fix it?

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean isInvalidResponse = (response.equalsIgnoreCase("no") || response.equalsIgnoreCase("no."));
do 
  response = console.readLine("Do you understand do while loops?   \n");
 while (isInvalidResponse = true);

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! On line 2 of your code you declare a String named response. This was not given an initial value. On the line immediately after that you're asking if the response is equal to "no", but response has no value. Response is not given a value until the line after do. Above and beyond that, you will also experience issues after you fix that as your while does not contain a condition, but rather an assignment

:bulb: Hint: Check if the response is equal to "no" on the same line as the while.

Hope this helps! :sparkles: