Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Cannot find error.

My workspace:

import java.io.Console;
public class Isaiah {  
public static void main(String[] args) {        
Console console = System.console();
boolean isSecretCode;
String isName;
String fatal;
String secretCode;
do {
isName = console.readLine("Enter your name:  ");
if(isName.equals("*ISAIAH*")){
secretCode = console.readLine("YOU HAVE ENTERED A SECRET CODE.\nHERE ARE YOUR OPTIONS.\n >IF YOU WANT TO CONTINUE:\nPRESS I.\n >IF YOU WANT TO EXIT:\n PRESS  E.\n  ");}
isSecretCode = (secretCode.equalsIgnoreCase("E"));
if(secretCode.equalsIgnoreCase("I")){fatal = console.readLine(" WARNING:\nTHIS PROGRAM CAN BE FATAL.\nDO YOU STILL WANT TO CONTINUE?\n\n  ");}
if(fatal.equalsIgnoreCase("yes")){console.readLine("Hello, Isaiah.\n");}
if(isSecretCode){console.printf("Loading...\n");}
}while(isSecretCode);
String number = console.readLine("Enter your favorite number:  ");
String mood = console.readLine("Enter your favorite mood:  "); 
String name = console.readLine("Enter a name:  ");
String creature = console.readLine("Enter your favorite creature:  ");
String color = console.readLine("Enter your favorite color:  ");
String isFavoriteThing = console.readLine("Is a %s, %s %s year old %s named %s your favorite thing?  ", mood, color, number,creature, name);
if(isFavoriteThing.equalsIgnoreCase("no")) {
console.readLine("Why not?  ");
console.printf("Well you should, it's all of your favorite stuff.\n\n");}
String computer = console.readLine("-------------------------------------------------\n  Do you want to have a talk with the computer?  ");
console.printf("\n---------------------------------------------\n\n\n");
if(computer.equalsIgnoreCase("yes")){
String howYouDoing = console.readLine("Hello %s, how are you doing?\n  ", isName);
if(howYouDoing.equalsIgnoreCase("sad") || howYouDoing.equalsIgnoreCase("awful")){
console.readLine("Oh no, %s why are you so sad?\n  ", isName);
}}}}

My error:

Isaiah.java:13: error: variable secretCode might not have been initialized
isSecretCode = (secretCode.equalsIgnoreCase("E"));
^
Isaiah.java:15: error: variable fatal might not have been initialized
if(fatal.equalsIgnoreCase("yes")){console.readLine("Hello, Isaiah.\n");}
^
2 errors

Thanks,

ISAIAH S

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Isaiah

From your debug it looks as though the error can be rectified by simply initializing the boolean variable isSecretCode with either a true or false value.

This error is caused because when compiled if the value of a variable is defined inside an if statement the compiler throws up a kind of red flag as if to say "hey this variable might never get assigned a value and I need to use it later."

It is good practice to assign variables a 'dummy' value so they have some meaning to the program and alter them accordingly as needed. I would change the following lines:-

boolean isSecretCode = true;
String isName = "";
String fatal = "";
String secretCode = "";

This enirely depends on what you need those variables to contain

Hope this helps

Daniel

It DID help!

thanks!!!!