1

I've been trying to make a choose your adventure type of program, but I've run into a problem. I have the whole thing running in a do while loop, and each option is an element in an array is an element. I also have if statements in the do while that can change some elements depending on what the user has already done. Here's an example of what my code looks like:

import java.util.Scanner;

public class MainClass {
    public static void main(String[] args) {
        Scanner input;
        input=new Scanner(System.in);
        boolean run;
        boolean theBoo = false;
        run = true;
        int choice;
        choice = 0;

        do {
            String[] theArray;
            theArray = new String[2];
            theArray[0] = "Hello";

            if(theBoo){
                theArray[1] = "Goodbye";
            }
            else{
                theArray[1] = "Hi";
                theBoo = true;
            }
            System.out.println(theArray[choice]);
            choice = input.nextInt();

        } while(run);
    }
}

For some reason though, it prints out "Goodbye" if you input 1, even though it should print "Hi", as theBoo is false. My question is: why is the do while loop changing the value of my variable, and how do I prevent it from doing so? Thanks!

Edit: Btw, I am new here, so my apologies if I did something wrong.

Edit2: First of all, thanks everyone for the quick answers. I made the changes you recommended, but its still doing the same thing. I updated the code to what it is with the changes.

5
  • Use == for comparisons. Commented Jan 15, 2014 at 22:14
  • What @ZouZou says is not a hint, it's the root cause of your problem! Commented Jan 15, 2014 at 22:16
  • You could use IDE for code formatting, it's very hard to read the original post. Many errors come from low code readability. Commented Jan 15, 2014 at 22:17
  • Why do people insist on using if(boolean == true). It is fairly obvious that the if statement evaluates a boolean so it should be obvious that if(boolean) is the correct construction. This also has be nice feature of preventing the if(boolean = true) which is what we have here. Commented Jan 15, 2014 at 22:17
  • This time I formatted source code for you, but don't count on that. People here are not likely to waste even 10 seconds trying to make sense out of a mess of curly braces wildly laid around. Please be kind towards other people and always provide clean, nicely formatted code. Commented Jan 15, 2014 at 22:18

4 Answers 4

2

update your code to

if(theBoo){
    theArray[1] = "Goodbye";
}
else{
    theArray[1] = "Hi";
    theBoo = true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Use == and not = to test for equality. In fact, for Booleans you don't need any explicit equality signs. You can just have:

if(theBoo)
      //something
else
      //something else

Notice how you don't have two ifs. Not only is it unnecessary, but it doesn't make logical sense to test (theBoo==true) and then if(theBoo==false) (where, again, you can completely omit the ==true or ==false parts). If the first condition is true, the second can't (as it's the opposite of the first), so you really want an else or else if there.

Same with the == for the while condition at the bottom. You can just have:

while(run)

Another comment - you often don't have any need to define the variable on a separate line from the declaration. You can just have:

boolean theBoo = false;

instead of:

boolean theBoo;
theBoo = false;

It's shorter and cleaner the first way, on one line.

Comments

1

Your using one = sign, you're setting the value of theBoo to true, not testing its value

Use == for tests

fixed code

import java.util.Scanner;
public class MainClass {

public static void main(String[] args) {
    Scanner input;
    input=new Scanner(System.in);
boolean run;
boolean theBoo;
theBoo = false;
run = true;
int choice;
choice = 0;

do{
String[] theArray;
theArray = new String[2];
theArray[0] = "Hello";

if(theBoo == true){
    theArray[1] = "Goodbye";
}
if(theBoo == false){
    theArray[1] = "Hi";
    theBoo = true;
}
System.out.println(theArray[choice]);
choice = input.nextInt();

}while(run == true);
}
}

Comments

0

This is one of those cases in Java where the static typing doesn't prevent a silly mistake.

The expression x = v is always an assignment and the result has the same type as that of the v expression. Unfortunately, in this case it means that the boolean-typed result doesn't throw a type error when used with the conditional ..

boolean a = true;
// a -> true
boolean b = a = false;  // boolean b = (a = false);
// a -> false
// b -> false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.