-2

I am a beginner in java and trying to learn it by my own. my code is not working. can anybody please help me find out the error. Along with if anyone can tell me the best tutorials to learn java.

import java.util.Scanner;

public class TwentyQue {

public static void main(String args[]){

    String first="", second="";
    String x="animal",vegetable,mineral;
    Scanner s=new Scanner(System.in);


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

    System.out.println("Is it animal, vegetable, or minaral?");
    first=s.next();

    if (first==x){
        System.out.println("Is it bigger than a breadbox");
        second=s.next();
        if(second=="yes"){
            System.out.println("My guess id that you are thinking of a mouse");
            System.out.println("I would ask you but i really dont care.");
        }else{
            System.out.println("My guess id that you are thinking of a squirrel");
            System.out.println("I would ask you but i really dont care.");

        }

    }else if (first=="vegetable"){
        if(second=="yes"){
            System.out.println("My guess id that you are thinking of a watermelon");
            System.out.println("I would ask you but i really dont care.");
        }else{
            System.out.println("My guess id that you are thinking of a carrot");
            System.out.println("I would ask you but i really dont care.");

        }}else if (first=="mineral"){
            if(second=="yes"){
                System.out.println("My guess id that you are thinking of a Camero");
                System.out.println("I would ask you but i really dont care.");
            }else{
                System.out.println("My guess id that you are thinking of a paper clip");
                System.out.println("I would ask you but i really dont care.");

            }

        }




}

}

3 Answers3

1

Use .equals(String s) to compare strings' content, don't use ==.

Use == only when you want to compare the identity of the object.


You can change the comparison of Strings to:

if (first.equals(x)){
  //...
}
else if (first.equals("vegetable")){
 //...
}

Apply the same for your other String comparisons.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • You actually should be doing "vegetable".equals(first), this will prevent NPE when first is null. – agilob Mar 09 '16 at 09:30
  • 1
    @agilob - That is nowadays considered an [antipattern](http://pushinginertia.com/2011/05/why-yoda-conditions-are-bad-and-usage-of-javas-final-keyword-is-good/). – OldCurmudgeon Mar 09 '16 at 09:33
  • i have tried that but the code below id not printing. if("yes".equals(second)){ System.out.println("My guess id that you are thinking of a mouse"); System.out.println("I would ask you but i really dont care."); – Manoj Kumar Mar 09 '16 at 09:35
  • @OldCurmudgeon avoiding NPE's an antipattern? First thing I've heard of it. – Stultuske Mar 09 '16 at 09:38
  • @ManojKumar What is your first and second input? – user3437460 Mar 09 '16 at 09:41
  • @OldCurmudgeon SonarQube is more convincing for me than this blog https://dev.eclipse.org/sonar/rules/show/squid:S1132 – agilob Mar 09 '16 at 09:51
0

1) Comparing String objects should happen using equals method. Ex :

str1.equals(str2)

If you use == , then it would check if the references are equals or not and not the values

Rishi
  • 1,163
  • 7
  • 12
0

the code worked when i did following changes. Thank you all for your help.

String x="animal";
    String y="yes";String x="animal";
    String y="yes";
if (x.equals(first)){

        if(y.equals(second)){
            System.out.println("My guess id that you are thinking of a mouse");
            System.out.println("I would ask you but i really dont care.");
        }

That would be greatly appreciated if someone can tell me why it was not working when i did not intilise the variables with the desired value?