-4

I am looking for a little help. I am sitting at home trying to learn java programming from programmingbydoing.com. I need to make a program called TwentyQuestions. The code is missing something to make it work completely. I have tried to find help other places on the web. That is why the code looks much like other peoples code.

import java.util.Scanner;

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

        String type, size, guess = "";

        System.out.println("TWO QUESTIONS!");
        System.out.println("Think of an object, and I'll try to guess it.");
        System.out.println("");

        System.out.println("Question 1) Is it animal, vegetable or mineral?");
        System.out.print("> ");
        type = keyboard.next();
        System.out.println("");

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

        if (type.equalsIgnoreCase("animal"))
        {
            if (size == "no")
            {
                guess = "squirrel";
            }
            else
            {
                guess = "moose";
            }
        }

        else if (type.equalsIgnoreCase("vegetable"))
        {
            if (size == "no")
            {
                guess = "carrot";
            }
            else
            {
                guess = "watermelon";
            }
        }

        else if (type.equalsIgnoreCase("mineral"))
        {
            if (size == "no")
            {
                guess = "paper clip";
            }
            else
            {
                guess = "Camero";
            }
        }

        if (guess == "")
        {
            System.out.println("The answers provided are not valid. Please try again.");
        }

        else
        {
            System.out.println("My guess is that you are thinking of a " + guess + ".");
            System.out.println("I would ask you if I'm right, but I don't actually care.");
        }
  }
}

The problem is that the program does not care about the comparisons that are in the nested if-statements. Control always goes to the else and prints that one.

Mark van Straten
  • 9,287
  • 3
  • 38
  • 57
  • http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – GriffeyDog Jan 12 '17 at 20:29
  • Keep in mind that `size` is a String, and so is `type`. Compare your `if` statement for `type` (which is working) to the `if` statement for `size` (which isn't working) and the answer will be clear to you. I'd also apply the same logic to `guess`. – Michael Dodd Jan 12 '17 at 20:38
  • Your first else has a pair of brackets. Since it's not an if else it shouldn't. – Wade Tyler Jan 12 '17 at 20:39
  • Why do you compare Strings correctly in one part of your code and incorrectly elsewhere? It's as if you're borrowing code but are not understanding it. – Hovercraft Full Of Eels Jan 12 '17 at 20:59

1 Answers1

-1

when you want to compare Strings you need to use the method .equals() or in the case to ignore case .equalsIgnoreCase()

Try the code below and you'll see it works just fine.

import java.util.Scanner;

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

    String type, size, guess = "";

    System.out.println("TWO QUESTIONS!");
    System.out.println("Think of an object, and I'll try to guess it.");
    System.out.println("");

    System.out.println("Question 1) Is it animal, vegetable or mineral?");
    System.out.print("> ");
    type = keyboard.next();
    System.out.println("");

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

    if (type.equalsIgnoreCase("animal"))
    {
        if (size.trim().equalsIgnoreCase("no"))
        {
            guess = "squirrel";
        }
        else 
        {
            guess = "moose";
        }
    }

    else if (type.equalsIgnoreCase("vegetable"))
    {
        if (size.trim().equalsIgnoreCase("no"))
        {
            guess = "carrot";
        }
        else
        {
            guess = "watermelon";
        }
    }

    else if (type.equalsIgnoreCase("mineral"))
    {
        if (size.trim().equalsIgnoreCase("no"))
        {
            guess = "paper clip";
        }
        else
        {
            guess = "Camero";
        }
    }

    if (guess == "")
    {
        System.out.println("The answers provided are not valid. Please try again.");
    }

    else
    {
        System.out.println("My guess is that you are thinking of a " + guess + ".");
        System.out.println("I would ask you if I'm right, but I don't actually care.");
    }
  }
}
Douglas
  • 36
  • 1
  • 5