Прочитать входной файл из основного класса и добавить ребра для другого класса - PullRequest
0 голосов
/ 05 августа 2020

Я пытаюсь прочитать входной файл в Main. java без использования GUI и добавить края в arrayList в Graph. java. В классе Main - graph.addEdge (edge ​​[0], edge [1]); метод не работает. Что я делаю не так?

public class Main {

    private Graph graph = new Graph();

    public static void main(String args[]) {

        JFileChooser choice = new JFileChooser(new File("."));
        int option = choice.showOpenDialog(null);
        if (option == JFileChooser.APPROVE_OPTION)
            try {
                Scanner input = new Scanner(choice.getSelectedFile());
                while (input.hasNextLine()) {
                    String edgeString = input.nextLine();
                    String[] edge = edgeString.split(" ");
                    graph.addEdge(edge[0], edge[1]);
                }
            } catch(FileNotFoundException exception){
            };
        }
    }


//Graph class with addEdge method

public class Graph<V> {

    private int V;  // No. of vertices

    // An Array of List which contains references to the Adjacency List of each vertex
    List<Integer> adj[];

    public Graph() {
    }

    // Constructor 
    public Graph(int V) {

        this.V = V;
        adj = new ArrayList[V];
        for (int i = 0; i < V; i++)
            adj[i] = new ArrayList<Integer>();
    }

    // This function adds the edge between source to destination
    public void addEdge(int u, int v) {

        adj[u].add(v);
    }
...