2017-05-15 9 views
0

これはファイルからのJavaバイナリ検索ツリーのコードです。このコードは、csvファイルからバイナリ検索ツリーを作成しています。しかし、私はこのコードを実行するとエラーが発生します。と言われています。ここで エラー:スレッド "main"の例外java.util.NoSuchElementException

**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); 

    } 

} 

uのみんなは、私はこの問題を解決する:(

+0

あなたのファイルはどのように見えますか、どのような情報がありますか? –

+0

'line'にスペースが含まれていないように見えます。したがって、2番目のトークンはありません。 csvファイルの –

+0

@YCF_Lそれは国の名前と国の人口を含んでいます。 –

答えて

0

をあなたは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) { 
      throw new NoSuchElementException(); 
     } else { 
      int arg0 = this.currentPosition; 
      this.currentPosition = this.scanToken(this.currentPosition); 
      return this.str.substring(arg0, this.currentPosition); 
     } 
    } 

あなたが入力ファイル "C:\ Users \ 8Users \ Desktop/countries.txt"内に何らかの問題があります。

関連する問題