-1

The user has to choose which tree to use:

Which tree would you like to test (BST, ST, RBT)?

I thought you could just take the input as a string and compare it to a string to pick which tree to use but I think im wrong, below is what ive done

    Scanner input = new Scanner(System.in);
    System.out.println("Which tree would you like to test (BST, ST, RBT)? ");
    treeChoice = input.nextLine();

    if(treeChoice == "BST")
    {
        myTree = new BST<Integer>();
    }
    else if(treeChoice == "ST")
    {
        //ST<Integer> myTree = new ST<Integer>();
    }
    else if(treeChoice == "RBT")
    {
        //RBT<Integer> myTree = new RBT<Integer>();
    }
    else
    {
        System.out.println("Invalid Entry");
    }

That doesnt seem to be working, when i tested it the output was invalid entry.

am I going about this the wrong way?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
alexthefourth
  • 127
  • 1
  • 11
  • 23

2 Answers2

3

if(treeChoice == "BST")

should be

if("BST".equalsIgnoreCase(treeChoice))

and modify the rest accordingly. The rule is do not use == for String comparison

iTech
  • 18,192
  • 4
  • 57
  • 80
1

You have to use equals method for comparisons of two strings. I suggest you to use switch case. Java 7 support string in switch case. This will improve your code readability and can handle all inputs from user.

MayurB
  • 3,609
  • 2
  • 21
  • 36