0

I've just started java couple of days ago. Im currently following this 'course' http://programmingbydoing.com . Havent had encountered any problems yet but now im stuck at task 32.

heres my code so far (always getting Squirrel instead of moose as output):

import java.util.Scanner;

public class TwoQuestion32 {

public static void main(String[] args) {
    boolean animal, vegetable, mineral, smallerthan;

    String whatIsIt, biggerThan;
    Scanner keyboard = new Scanner(System.in);


    System.out.println("Hello and welcome, i've got 2 questions for you!");
    System.out.println("Think of an object and i'll try to guess it");
    System.out.println();
    System.out.println("Question 1) Is it an animal, vegetable or mineral?");
    System.out.print(">");
    whatIsIt = keyboard.nextLine();

    if (whatIsIt == "animal")
            animal = true;
            if (whatIsIt == "vegetable")
            vegetable = true;
            if (whatIsIt == "mineral")
            mineral = true;


    System.out.println("Question 2) Is it bigger than a breadbox?");
    System.out.print(">");
        biggerThan = keyboard.nextLine();
            if (biggerThan == "yes")
            smallerthan = false;
            if (biggerThan == "no"){
            smallerthan = true;}


            System.out.print("My guess is that you are thinking of a ");
        if (animal = true){
            if (smallerthan = true)
                System.out.println("squirrel");
        }else { 
                System.out.println("moose");}
}   
}

Thanks in advance! Would also love to hear tips how to put up the code in smarter ways. Be friendly, keep in mind i've just started!

Edit: Okay I took another approach. My first attempt was really strange. Thanks for the help!

Heres the working code:

import java.util.Scanner;

public class Questions32 {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);


    String whatIsIt, whatIsIt2;
    String animal = "animal";
    String mineral = "mineral";
    String vegetable = "vegetable";
    String bigger = "yes";
    String smaller = "no";


    System.out.println("Hello and welcome, i've got 2 questions for you!");
    System.out.println("Think of an object and i'll try to guess it");
    System.out.println();
    System.out.println("Question 1) Is it an animal, vegetable or mineral?");
    System.out.print(">");
    whatIsIt = keyboard.nextLine();

    System.out.println("Question 2) Is it bigger than a breadbox?");
    System.out.print(">");
        whatIsIt2 = keyboard.nextLine();

    if (whatIsIt.equalsIgnoreCase(animal)){
        if (whatIsIt2.equalsIgnoreCase(bigger)){
            System.out.println("My guess is that you are thinking of a moose");
        }else{ System.out.println("My guess is that you are thinking of a squirrel");
        }
    }

    if (whatIsIt.equalsIgnoreCase(vegetable)){
        if (whatIsIt2.equalsIgnoreCase(bigger)){
            System.out.println("My guess is that you are thinking of a melon");
        }else{ System.out.println("My guess is that you are thinking of a carrot");
        }
    }

    if (whatIsIt.equalsIgnoreCase(mineral)){
        if (whatIsIt2.equalsIgnoreCase(bigger)){
            System.out.println("My guess is that you are thinking of a Camaro");
        }else{ System.out.println("My guess is that you are thinking of a paper clip");
        }
    }
    System.out.println("I would ask you if I'm right, but I dont actually care.");

    }


}
MrMeeseeks
  • 11
  • 3
  • 1
    Don't compare Strings with `==` or `!=`. Understand that this compares *references* -- that the two String variables refer to the same String *object* and that's not what you want. Instead use either the String `equals(...)` or its `equalsIgnoreCase(...)` methods which perform a *functional equality* test -- that the Strings have the same characters in the same order -- which is exactly what you want. – Hovercraft Full Of Eels Apr 12 '17 at 17:09
  • Also read up on the difference between assignment and relational equality. For a bonus read up on yoda expressions. Even though only a 3am debugging session permanently reinforces the difference. – Bathsheba Apr 12 '17 at 17:18
  • 1
    Not sure what IDE you're using, but you should've gotten a big fat warning about doing assignment inside an if-condition. Ignoring those warnings is a surefire way to introduce bugs, especially as a beginner. – Patrick Parker Apr 12 '17 at 17:26
  • Okay, equalsIgnoreCase(...) works just fine. Im still a little bit confused about the "dont do assignment inside an if condition". So through the way i did it im always setting my booleans true? Whats the best way to bypass that? btw im using Eclipse with 1.8 and im not getting any warnings regarding these issues – MrMeeseeks Apr 12 '17 at 17:37

1 Answers1

1

in your if-statement you are setting animal to true every time if (animal = true){, instead of checking it (==) . Also, for Strings you must use .equals() instead of ==.

aap
  • 55
  • 10