-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
  • 118,400
  • 24
  • 176
  • 257
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
  • 17,764
  • 4
  • 54
  • 78
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,539
  • 2
  • 20
  • 36