无法在另一个while循环中读取while循环中创建的地图

时间:2015-12-16 13:05:26

标签: java

我正在尝试在while循环中创建一个对象映射,并在其他循环中使用它:

int x = 0;
    Map<String, Node> map = new HashMap<>();
    BufferedReader br = new BufferedReader(new FileReader("text.txt"));
    String line;
    while ((line = br.readLine()) != null) {
        if (line.contains("(") == true) {
            StringTokenizer st;
            st = new StringTokenizer(line, "()");
            String noden = st.nextToken();
            int heu = Integer.parseInt(st.nextToken());
            x++;
            //System.out.println(noden + "=" + heu);
            map.put("n" + x, new Node(noden, heu));
        }
    }

我可以在此循环之外访问此映射并获取存储在其中的数据,但是当我尝试在另一个循环中使用它时,它是空的:

while ((line = br.readLine()) != null) {
        if (line.contains("->") == true) {
            StringTokenizer st;
            st = new StringTokenizer(line, "-> ,");
            for (Map.Entry<String, Node> entry : map.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            }
            String Source_node = st.nextToken();
            String Dest_node = st.nextToken();
            //System.out.println(Source_node + "->" + Dest_node);
            for (int i = 1; i <= x; i++) {
                Node n1 = map.get("n" + i);
                System.out.println(n1.value);
            }
            int eweight = Integer.parseInt(st.nextToken());
            for (int i = 1; i <= x; i++) {
                Node n1 = map.get("n" + i);
                System.out.println(n1.value);
            }

            for (int i = 1; i <= x; i++) {
                Node node_source = map.get("n" + i);
                Node node_dest = map.get("n" + i);
                if (Source_node.equals(node_source.value)) {
                    if (Dest_node.equals(node_dest.value)) {
                        node_source.adjacencies = new Edge[]{
                            new Edge(node_dest, eweight)
                        };
                        System.out.println(node_source.pos);
                    }
                }
            }
        }
    }

整个方法

public static void main(String[] args) throws FileNotFoundException, IOException {
    int x = 0;
    Map<String, Node> map = new HashMap<>();
    BufferedReader br = new BufferedReader(new FileReader("text.txt"));
    String line;
    while ((line = br.readLine()) != null) {
        if (line.contains("(") == true) {
            StringTokenizer st;
            st = new StringTokenizer(line, "()");
            String noden = st.nextToken();
            int heu = Integer.parseInt(st.nextToken());
            x++;
            //System.out.println(noden + "=" + heu);
            map.put("n" + x, new Node(noden, heu));
        }
    }

    while ((line = br.readLine()) != null) {
        if (line.contains("->") == true) {
            StringTokenizer st;
            st = new StringTokenizer(line, "-> ,");
            for (Map.Entry<String, Node> entry : map.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            }
            String Source_node = st.nextToken();
            String Dest_node = st.nextToken();
            //System.out.println(Source_node + "->" + Dest_node);
            for (int i = 1; i <= x; i++) {
                Node n1 = map.get("n" + i);
                System.out.println(n1.value);
            }
            int eweight = Integer.parseInt(st.nextToken());
            for (int i = 1; i <= x; i++) {
                Node n1 = map.get("n" + i);
                System.out.println(n1.value);
            }

            for (int i = 1; i <= x; i++) {
                Node node_source = map.get("n" + i);
                Node node_dest = map.get("n" + i);
                if (Source_node.equals(node_source.value)) {
                    if (Dest_node.equals(node_dest.value)) {
                        node_source.adjacencies = new Edge[]{
                            new Edge(node_dest, eweight)
                        };
                        System.out.println(node_source.pos);
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

程序从不进入第二个while循环,因为BufferedReader br已经读取了第一个循环中的所有行。因此,br.readLine()将始终返回null

一个简单的解决方法是在两个while循环之间创建一个新的BufferedReader。

br = new BufferedReader(new FileReader("text.txt")); 

但是,我会考虑找到一种不同的方法,因为两次读取同一文件并不是最佳的。