简体   繁体   中英

Why will my program not go into my while loop?

I'm working on a program that will take a name and number of a periodic element, store it in a tree and then print it out in different orders.

When I try and run my main class it asks for the name, but then it doesn't go into the while loop.

Here is my main class.

public class BinarySearchTree 
{

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

        String name;
        int atomicNum;

        BSTInterface<PeriodicElement> elements = new BSTree<PeriodicElement>();
        PeriodicElement element;
        int numElements;

        String skip;

        System.out.print("Element name (press Enter to end): ");
        name = conIn.nextLine();
        while (!name.equals(""));
        {
            System.out.print("Atomic Number: ");
            atomicNum = conIn.nextInt();
            skip = conIn.nextLine();

            element = new PeriodicElement(name, atomicNum);
            elements.add(element);

            System.out.print("Element name (press ENTER TO END): ");
            name = conIn.nextLine();
        }
        System.out.println();
        System.out.println("Periodic Elements");

        numElements = elements.reset(BSTree.INORDER);
        for (int count = 1; count <= numElements; count++)
        {
            System.out.println(elements.getNext(BSTree.INORDER));
        }

    }

}

Here is my Binary Search Tree

public class BSTree <T extends Comparable<T>>
                implements BSTInterface<T>
{
    protected BSTNode<T> root;
    boolean found;

    protected LinkedUnbndQueue<T> inOrderQueue;
    protected LinkedUnbndQueue<T> preOrderQueue;
    protected LinkedUnbndQueue<T> postOrderQueue;

    public BSTree()
    {
        root = null;
    }

    public boolean isEmpty()
    {
        return (root == null);
    }

    private int recSize(BSTNode<T> tree)
    {
        if(tree == null)
        {
            return 0;
        }
        else
        {
            return recSize(tree.getLeft()) + recSize(tree.getRight()) + 1;
        }
    }

    public int size()
    {
        int count = 0;
        {
            if(root != null)
            {
                LinkedStack<BSTNode<T>> hold = new LinkedStack<BSTNode<T>>();
                BSTNode<T> currNode;
                hold.push(root);
                while(!hold.isEmpty())
                {
                    currNode = hold.top();
                    hold.pop();
                    count++;
                    if(currNode.getLeft() != null)
                    {
                        hold.push(currNode.getLeft());
                    }
                    if(currNode.getRight() != null)
                    {
                        hold.push(currNode.getRight());
                    }
                }
            }
            //System.out.println(count);
            return count;
        }
    }

    public boolean recContains(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            return false;
        }
        else if(element.compareTo(tree.getInfo()) < 0)
        {
            return recContains(element, tree.getLeft());
        }
        else if(element.compareTo(tree.getInfo()) > 0)
        {
            return recContains(element, tree.getRight());
        }
        else
        {
            return true;
        }
    }

    public boolean contains (T element)
    {
        //System.out.println("Tree contains: " + recContains(element, root));
        return recContains(element, root);
    }

    public T recGet(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            return null;
        }
        else if(element.compareTo(tree.getInfo()) < 0)
        {
            return recGet(element, tree.getLeft());
        }
        else if(element.compareTo(tree.getInfo()) > 0)
        {
            return recGet(element, tree.getRight());
        }
        else
        {
            return tree.getInfo();
        }
    }

    public T get(T element)
    {
        //System.out.println(recGet(element, root));
        return recGet(element, root);
    }

    public void add(T element)
    {
        root = recAdd(element, root);
    }

    private BSTNode<T> recAdd(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            tree = new BSTNode<T>(element);
        }
        else if(element.compareTo(tree.getInfo()) <= 0)
        {
            tree.setLeft(recAdd(element, tree.getLeft()));
        }
        else
        {
            tree.setRight(recAdd(element, tree.getRight()));
        }
        return tree;
    }

    public boolean remove(T element)
    {
        root = recRemove(element, root);
        return found;
    }

    private BSTNode<T> recRemove(T element, BSTNode<T> tree)
    {
        if(tree == null)
        {
            found = false;
        }
        else if (element.compareTo(tree.getInfo()) < 0)
        {
            tree.setLeft(recRemove(element, tree.getLeft()));
        }
        else if (element.compareTo(tree.getInfo()) > 0)
        {
            tree.setRight(recRemove(element, tree.getRight()));
        }
        else
        {
            tree = removeNode(tree);
            found = true;
        }
        return tree;
    }

    private BSTNode<T> removeNode(BSTNode<T> tree)
    {
        T data;

        if(tree.getLeft() == null)
        {
            return tree.getRight();
        }
        else if(tree.getRight() == null)
        {
            return tree.getLeft();
        }
        else
        {
            data = getPredecessor(tree.getLeft());
            tree.setInfo(data);
            tree.setLeft(recRemove(data, tree.getLeft()));
            return tree;
        }
    }

    private T getPredecessor(BSTNode<T> tree)
    {
        while (tree.getRight() != null)
        {
            tree = tree.getRight();
        }
        return tree.getInfo();
    }

    public int reset(int orderType)
    {
        int numNodes = size();

        if(orderType == INORDER)
        {
            inOrderQueue = new LinkedUnbndQueue<T>(numNodes);
        }
        else
        {
            if(orderType == PREORDER)
            {
                preOrderQueue = new LinkedUnbndQueue<T>(numNodes);
                preOrder(root);
            }
            if(orderType == POSTORDER)
            {
                postOrderQueue = new LinkedUnbndQueue<T>(numNodes);
                postOrder(root);
            }
        }
        return numNodes;
    }

    public T getNext(int orderType)
    {
        if(orderType == INORDER)
        {
            return inOrderQueue.dequeue();
        }
        else
        {
            if(orderType == PREORDER)
            {
                return preOrderQueue.dequeue();
            }
            else
            {
                if(orderType == POSTORDER)
                {
                    return postOrderQueue.dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
    }

    private void inOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            inOrder(tree.getLeft());
            inOrderQueue.enqueue(tree.getInfo());
            inOrder(tree.getRight());
        }
    }

    private void preOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            preOrderQueue.enqueue(tree.getInfo());
            preOrder(tree.getLeft());
            preOrder(tree.getRight());
        }
    }

    private void postOrder(BSTNode<T> tree)
    {
        if(tree != null)
        {
            postOrder(tree.getLeft());
            postOrder(tree.getRight());
            postOrderQueue.enqueue(tree.getInfo());
        }
    }
}

Here is the Binary Search Tree Node class

public class BSTNode <T extends Comparable<T>>
{
    protected T info;
    protected BSTNode<T> left;
    protected BSTNode<T> right;

    public BSTNode(T info)
    {
        this.info = info;
        left = null;
        right = null;
    }

    public void setInfo(T info)
    {
        this.info = info;
    }

    public T getInfo()
    {
        return info;
    }

    public void setLeft(BSTNode<T> link)
    {
        left = link;
    }

    public void setRight(BSTNode<T> link)
    {
        right = link;
    }

    public BSTNode<T> getLeft()
    {
        return left;
    }

    public BSTNode<T> getRight()
    {
        return right;
    }
}

Remove the semi-colon that is terminating the while statement

while (!name.equals(""));
                        ^

在while语句之后删除半冒号,因为半冒号使它在此结束,而{如果某事是正确的(!)则是一个开始(!),并且显然应该以}结束。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM