错误:线程“main”java.util.NoSuchElementException中的异常



这是来自文件的Java Binary Search Tree的代码。该代码正在从 csv 文件创建一个二叉搜索树。但是当我运行此代码时我遇到了错误。是说。

**Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
at binarytree.Node.<init>(Node.java:28)
at binarytree.BinaryTree.main(BinaryTree.java:241)**

以下是字符串节点的代码:

    import java.util.StringTokenizer;
    public class Node {
    String key;
    int value;
    Node left;
    Node right;
    Node(String line )
    {
        // parse the string into variables
        StringTokenizer st = new StringTokenizer(line," ");
        this.key= st.nextElement().toString();
        this.value = Integer.parseInt(st.nextElement().toString());
    }
    Node(String key, int value) {
            this.key = key;
            this.value = value;
    }
    public String toString() {
        return key + " " + value;
    }
 }

这是树的代码,包括插入、搜索和排序。

    import java.io.FileReader;
    import java.io.IOException;
public class BinaryTree {
    Node root;
    int MAX = 0;
    int MIN = 0;
    /***
* Add Node to the tree
*@param Country Name
*@param Population
     */
    public void addNode(String key, int value) {
        // Create a new Node and initialize it
        Node newNode = new Node(key, value);
        // If  no root this will become root
        if (root == null) {
            root = newNode;
        } else {
            // Set root as the Node and start  traversing the tree
            Node focusNode = root;
            // Future parent for new Node
            Node parent;
            while (true) {
                // root is the top parent 
                parent = focusNode;
        // Check if the new node should go on the left of parent
                if (key.compareToIgnoreCase(focusNode.key) < 0) {
                    // focus to the left child
                    focusNode = focusNode.left;
                    // If the left child has no children
                    if (focusNode == null) {
                        // then place the new node on the left of it
                        parent.left = newNode;
                        return; 
                    }
                } else { 
                    // If we get here put the node on the right
                    focusNode = focusNode.right;
                    // If the right child has no children
                    if (focusNode == null) {
                        // then place the new node on the right of it
                        parent.right = newNode;
                        return; 
                    }
                }
            }
        }
    }
    /***
* Order the tree ascending by key
*@param node
*@return
     */
    public Node orderTreeByCountry(Node node)
    {
        if(node != null)
        {
            orderTreeByCountry(node.left);
            System.out.println(node);
            orderTreeByCountry(node.right);
        }
        return node;
    }
    /***
* Find node by providing key name
*@param Node
*@return Node
     */
    public Node findNode(String key) {
        // Start at the top of the tree
        Node focusNode = root;
        // While node not found keep looking
        while (focusNode.key.compareToIgnoreCase(key) != 0) {
            // If we should search to the left
            if (key.compareToIgnoreCase(focusNode.key) < 0) {
                // Shift the focus Node to the left child
                focusNode = focusNode.left;
            } else {
                // Shift the focus Node to the right child
                focusNode = focusNode.right;
            }
            // The node wasn't found
            if (focusNode == null)
                return null;
        }
        return focusNode;
    }
    /***
* Get the value of node with specific key
*@param key
*@return Value
     */
    public int getValueForKey(String key)
    {
        // Start at the top of the tree
                Node focusNode = root;
                // While node not found
                while (focusNode.key.compareToIgnoreCase(key) != 0) {
                    // If we should search to the left
                    if (key.compareToIgnoreCase(focusNode.key) < 0) {
                        // Shift the focus Node to the left child
                        focusNode = focusNode.left;
                    } else {
                        // Shift the focus Node to the right child
                        focusNode = focusNode.right;
                    }
                    // The node wasn't found
                    if (focusNode == null)
                        return -1;
                }
                return focusNode.value;
    }
    /***
* Get how many nodes in the tree
*@param node
*@return number of nodes
     */
    public int getNodesCount(Node node)
    {
        if(node == null) {
            return 0;
        } else {
            int count = 1;
            count  +=  getNodesCount(node.left);
            count  += getNodesCount(node.right); 
            return count;
        }
    }
    /***
* Get min value for tree which is based on key, value attributes
*@param node
*@return min value
     */
    public int getMinValue(Node node)
    {   
        if(node != null)
        {   
            getMinValue(node.left); 
            if(MIN == 0 ) {
                MIN = node.value;
            }
            if(MIN > node.value) {
                MIN = node.value;
            }
            getMinValue(node.right);
        }
        return MIN;
    }
    /***
* Get max value for tree which is based on key, value attributes
*@param node
*@return max value
     */
    public int getMaxValue(Node node)
    {
        if(node != null)
        {   
            getMaxValue(node.left); 
            if(MAX == 0 ) {
                MAX = node.value;
            }
            if(MAX < node.value) {
                MAX = node.value;
            }
            getMaxValue(node.right);
        }
        return MAX;
    }
    public static void main(String[] args)
    {
        BufferedReader br;
        BinaryTree btree = new BinaryTree();
        try {
            br = new BufferedReader(new 
        FileReader("C:\Users\8Users\Desktop/countries.txt"));
            String line = "";
             while ((line = br.readLine()) != null) { 
                    /*Create Country node from the line */
                    Node country = new Node(line); 
                    btree.addNode(country.key, country.value);          
                }
             br.close();
        } 
        catch (FileNotFoundException e) {
            System.out.println(e.getLocalizedMessage());
        } 
        catch (IOException e) {
            System.out.println(e.getLocalizedMessage());
        }
        btree.orderTreeByCountry(btree.root);
        System.out.println("Number of Countries is : " + 
        btree.getNodesCount(btree.root));
        System.out.println("Min Population is : " + 
        btree.getMinValue(btree.root));
        System.out.println("Max Population is : " + 
        btree.getMaxValue(btree.root));
        int population = btree.getValueForKey("xxx");
       if(population== -1)
            System.out.println("Sorry this Country is not in this btree");
        else
            System.out.println("Population is : " + population);
    }
}

希望你们能帮我解决这个问题:(

如果你在java.util中看到nextToken((methon的实现。StringTokenizer 类如下:

   public String nextToken(( {        this.currentPosition = this.newPosition>= 0 && !this.delimsChanged ?this.newPosition: this.skipDelimiters(this.currentPosition(;        this.delimsChanged = false;        this.newPosition = -1;        if (this.currentPosition>= this.maxPosition( {            抛出新的 NoSuchElementException((;        } else {            int arg0 = this.currentPosition;            this.currentPosition = this.scanToken(this.currentPosition(;            返回this.str.substring(arg0, this.currentPosition(;        }    }

正如你在上面看到的,方法抛出NoSuchElementException"if (this.currentPosition>= this.maxPosition("所以你的输入文件"C:\Users\8Users\Desktop/Country.txt"中一定存在一些问题。

相关内容

  • 没有找到相关文章

最新更新