Bummer! You have been redirected as the page you requested could not be found.

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

THIS CODE ISN'T DOING WHAT I WANT IT TO!!!!!!!!

What I want my code to do is:

Enter your name:

*(if the above is *ISAIAH it will say... YOU HAVE ENTERED A SECRET CODE. HERE ARE YOUR OPTIONS.

IF YOU WANT TO CONTINUE: PRESS I IF YOU WANT TO EXIT: PRESS E.)**

...Then it will run some more code.

Here's my code:

import java.io.Console;
public class Isaiah {  
public static void main(String[] args) {        
Console console = System.console();
boolean isSecretCode = true;
String isName = "";
do {
isName = console.readLine("Enter your name:  ");}while(isSecretCode);
if(isName.equals("*ISAIAH*")){
String 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")){String 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");}}
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")) {
console.readLine("Oh no %s, why are you so sad?  \n");
}}}}

But all it's doing is repeating:

Enter your name:

*(if the above is *ISAIAH it will say: YOU HAVE ENTERED A SECRET CODE. HERE ARE YOUR OPTIONS.

IF YOU WANT TO CONTINUE: PRESS I IF YOU WANT TO EXIT: PRESS E.)**

...and it only runs the other code if "secretCode" is "I" and "fatal" is "yes".

Anyone there?

3 Answers

The following code should do it. The thing is, that you did not need a loop for what you tried to accomplish. All you need is check if the input is "ISAIAH". If it is run the additional code, otherwise just run the code below. I hope this is what you tried to accomplish and that i could be of help. :)

import java.io.Console;

public class Isaiah {
  public static void main(String[] args) {
    Console console = System.console();

    String isName = console.readLine("Enter your name: ");

    if (isName.equals("*ISAIAH*")) {
      String 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  ");
      if (secretCode.equalsIgnoreCase("E")){
        System.exit(0);
      } else if (secretCode.equalsIgnoreCase("I")) {
        String fatal = console.readLine(" WARNING:\nTHIS PROGRAM CAN BE FATAL.\nDO YOU STILL WANT TO CONTINUE?\n\n  ");
        if (fatal.equalsIgnoreCase("yes")) {
          console.readLine("Hello, %s.\n", isName);
        } else {
          System.exit(0);
        }
      }
    }

    String number = console.readLine("Enter your favorite number:  ");
    String mood = console.readLine("Enter your favorite mood:  "); 
    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 your favorite thing?  ", mood, color, number,creature);

    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("H ello %s, how are you doing?\n  ", isName);
      if (howYouDoing.equalsIgnoreCase("sad")) {
        console.readLine("Oh no %s, why are you so sad?  \n");
      }  
    }    
  }
}

Hi there,

the problem lies within these lines:

boolean isSecretCode = true;
String isName = "";
do {
  isName = console.readLine("Enter your name:  ");
} while(isSecretCode);
if(isName.equals("*ISAIAH*")){

the value for isSecretCode will always be true since your if statement is outside the while loop. The following might do the trick.

boolean isSecretCode = true;
String isName = "";
do {
  isName = console.readLine("Enter your name:  ");
  if(isName.equals("*ISAIAH*")) {
    isSecretCode = false;
  }
} while(isSecretCode);

I hope this makes sense ;)

I changed:

import java.io.Console;
public class Isaiah {  
public static void main(String[] args) {        
Console console = System.console();
boolean isSecretCode = true;
String isName = "";
do {
isName = console.readLine("Enter your name:  ");}while(isSecretCode);
if(isName.equals("*ISAIAH*")){
String 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")){String 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");}}
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")) {
console.readLine("Oh no %s, why are you so sad?  \n");
}}}}

To:

import java.io.Console;
public class Isaiah {  
public static void main(String[] args) {        
Console console = System.console();
boolean isSecretCode = true;
String isName = "";
do {
isName = console.readLine("Enter your name:  ");
if(isName.equals("*ISAIAH*")){
isSecretCode = false;
String 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")){String fatal = console.readLine(" WARNING:\nTHIS PROGRAM CAN BE FATAL.\nDO YOU STILL WANT TO CONTINUE?\n\n  ");
if(fatal.equalsIgnoreCase("yes")){console.readLine("Hello, %s.\n", isName);}}
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 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 your favorite thing?  ", mood, color, number,creature);
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")) {
console.readLine("Oh no %s, why are you so sad?  \n");
}}}}

But I am getting the same thing as last time.

what your program does right now is repeat everything that is in the while block as long as the boolean "isSecretCode" equals true. The value for "isSecretCode" is only set to false when you input "ISAIAH". So with every other input name the loop starts again, meaning the first line inside the while loop is executed again:

do {
  isName = console.readLine("Enter your name:  ");
  . . .

I dont know if that is what you want the program to do. So it would be helpful if you could explain what you would like your code to do.

I want it to run the line

isName = console.readLine("Enter your name:  ");

and if the line above is ISAIAH then I want it to run the following lines:

String 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")){String 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");}

then, even if isName isn't ISAIAH, I want it to run:

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")) {
console.readLine("Oh no %s, why are you so sad?  \n");

But it's repeating isName until it's ISAIAH, then it will run the other code.