简体   繁体   English

如何使程序停止接受输入并运行程序?

[英]How can I get my program to stop accepting inputs and run the program?

I want the program to print out the adjacency list of the given input using maps and sets. 我希望程序使用地图和集合打印出给定输入的邻接列表。 The input itself is supposed to be a directed graph and each line is supposed to be an edge. 输入本身应该是有向图,每条线都应该是一条边。 I want the user to enter the input edge by edge and then enter a blank line to run the program. 我希望用户逐行输入输入,然后输入空白行以运行程序。 I can't test to see if it works because when I try to run the program and enter a blank line the cursor just moves to the next line and doesn't run the program. 我无法测试它是否有效,因为当我尝试运行该程序并输入空白行时,光标只会移至下一行而不运行程序。 I figure it has to do something with one of my while loops, but I've been tinkering for an hour or so with no luck. 我认为它必须与我的一个while循环有关,但我已经整整一个小时左右没有运气。 Anything helps! 有什么帮助!

import java.util.*;

public class AdjList {

    public static void main(String[] args) {

        Map<String, Set<String>> graph = new TreeMap<String, Set<String>>();

        ArrayList<String> lines = new ArrayList<String>();

        boolean control = true;
        while(control == true){
            Scanner in = new Scanner(System.in);
            if (in.nextLine().length() == 0){
                control = false;
            } else {
                while (in.hasNextLine()) {
                    lines.add(in.nextLine());
                    if (in.nextLine().length() == 0){
                      break;
                    }
                }   
                for (int i = 0; i < lines.size(); i++){
                    String line = lines.get(i);
                    String[] vertices = line.split(" "); 
                    if (graph.get(vertices[0]) == null){
                        Set<String> newSet = new HashSet<String>();
                        newSet.add(vertices[1]);
                        graph.put(vertices[0], newSet);
                    } else {
                        Set<String> oldSet = new HashSet<String>();
                        oldSet = graph.get(vertices[0]);
                        oldSet.add(vertices[1]);
                        graph.put(vertices[0], oldSet);
                    }
                }
            }   
        }
        for(String entry : graph.keySet()) {
            System.out.println(entry + ":" + graph.get(entry));
        }
    }
}

An example of what the input would be is: 输入的示例如下:

A B
C D
B D
E C
E B

And then entering an empty line to run. 然后输入一个空行来运行。 Let me know if you need any more info. 让我知道您是否需要更多信息。

This while loop is the problem: 这是while循环的问题:

while (in.hasNextLine()) {
    lines.add(in.nextLine());
    if (in.nextLine().length() == 0){
        break;
    }
}

Every time you do nextLine() it moves the file pointer forward so the next time you use nextLine() it look at the next line . 每次执行nextLine()它都会向前移动文件指针,因此,下次使用nextLine()它会查看下一行 So, in this while loop, it adds the line, moves to the next line and then checks if that line is empty. 因此,在此while循环中,它将添加行,移至下一行,然后检查行是否为空。 You should save the line, check if it's empty and if it's not, then add it to your ArrayList like so: 您应该保存该行,检查其是否为空,然后将其添加到ArrayList中,如下所示:

while (in.hasNextLine())
{
    String temp = in.nextLine();
    if (temp.length() == 0)
        break;
    else
        lines.add(temp);
}

Even with that said there are several other problems in your code. 即使这样说,您的代码中还有其他几个问题。 For example, it is unnecessary to have the while (control == true) loop. 例如,没有必要使用while (control == true)循环。 The only way control is ever false is if the very first line has no characters. 控制权永远为假的唯一方法是,如果第一行没有字符。 I'll let you find the rest of the errors on your own. 我会让您自己找到其余的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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