简体   繁体   中英

Infinite loop using a iterator over a treeset and I'm invoking the next() method

import java.util.*;
import java.io.*;

public class Assignment5 {

    public static void main(String[] args) throws FileNotFoundException
    {
        File sets=new File("test.txt");             //create file to read

        HashMap<String, TreeSet<String>> hm=new HashMap<String, TreeSet<String>>();                   //create hashmap

        TreeSet<String> allNodes=new TreeSet<String>();             //create a treeset to hold
                                                    //all nodes. No duplicates
        Scanner in=new Scanner(sets);

        while(in.hasNext())                         //while file has content
        {                                           //keep scanning it

            String node=in.next();                  //first value in each line
            String edge=in.next();                  //refers to node. Second
                                                    //value refers to an edge
                                                    //of the node

            allNodes.add(node);                     //keep track of all nodes
            allNodes.add(edge);                     //we come across

            if(!hm.containsKey(node))               //if the node is not already
            {
                TreeSet<String> newTemp=new TreeSet<String>();
                newTemp.add(edge);                                    //in the hash map then we
                hm.put(node, newTemp);                  //need to add a key and
            }                                       //map its first value

            else                                    //if the node is already in
            {                                       //the hashmap then we need
                TreeSet<String> temp=(TreeSet<String>)hm.get(node); //just add the new edge to
                temp.add(edge);                     //it
                hm.put(node, temp);
            }
        }

                System.out.println(allNodes.size());

            //we now have a hash map containg any nodes that have an edge with
            //a treeset showing all edges from the node

            int count=0;                            //we go through the treemap
                                                    //and test if all nodes
            Iterator iter=allNodes.iterator();      //have an edge.  If a node
            while(iter.hasNext());                  //in the file does not have
            {
                System.out.println("here?");
                String theKey=(String)iter.next();          //an edge then it is a leaf
                if(hm.containsKey(theKey))
                {
                    count++;
                }
            }

        System.out.println("we made it here too");

    }
}

It won't even print the "here?" message. I think it's an infinite loop but if it's not even executing the first instruction in the loop then how is it even getting stuck in the loop? What am I doing wrong? Any help would be greatly appreciated. It does print the expected size of the treeSet though.

EDIT :

A sample file for test.txt :

A B
B C
C D

The reason this doesn't work is because of the semicolon ( ; ) after the while loop:

while(iter.hasNext());
{
    //The rest of the code
}

This boils down to:

while(iter.hasNext()) {
    //no instructions
}
{
    //the rest of the code
}

The second part ( rest of the code ) is not even part of the loop, it is executed after the loop. By removing the semicolon, it will bind the sequence between the accolades ( { } ) to the while instruction.

As a result you don't call the .next() method at all in the loop, and thus keep polling whether there is a next element, but since you don't advance in the iterator, there will always be a next element.

You better never use a semicolon after a while loop , not even for a single instruction like:

while(condition)
    instruction;

Yes, this is valid Java. But based on experience, these things tend to eventually become hard to read. One better always uses accolades, to make it explicit that you execute only one, more or no instructions.

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