I'm having some trouble understanding why the Scanner is not picking up the "(" character when it is clearly there. When I tried to save my program, there was a pop-up that said that some of the characters couldn't be saved in the proper encoding. (I'm using Eclipse, and it uses Cp1252 encoding if that would be of any help) I changed it to UTF-8, and now I'm having these issues.
String input = "a 0 ( b 4 ( * 100 b 6 ) w 9 ( x 3 y 5 ( * 2 z 3 ) ) )";
LinkedStack<TreeNode<String>> stack = new LinkedStack<TreeNode<String>>();
Scanner in = new Scanner(input);
while (in.hasNext()) {
String temp = in.next();
System.out.println("Here's a temp:" + temp);
if (temp == "(") {
System.out.println("Continuing.");
continue;
}
else if (temp == ")") {
System.out.println("Closed Paren.");
TreeNode<String> leftChild = stack.pop();
TreeNode<String> rightChild = stack.pop();
TreeNode<String> parent = stack.pop();
parent.setLeft(leftChild);
parent.setRight(rightChild);
stack.push(parent);
}
else {
System.out.println("Hit the else block.");
String label = temp;
String distanceString = in.next();
double distance = Double.parseDouble(distanceString);
System.out.println("Here is the distance of this node: " +distance);
stack.push(new TreeNode<String>(label, null, null, distance));
}
}