简体   繁体   中英

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. 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 (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 . So, in this while loop, it adds the line, moves to the next line and then checks if that line is empty. You should save the line, check if it's empty and if it's not, then add it to your ArrayList like so:

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. 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.

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