命令行参数文件

时间:2016-04-26 00:53:13

标签: java command-line-arguments

我很难将代码更改为命令行参数。现在我还在用扫描仪阅读我的文字。我正在读书,我也需要读出来。

像:

 public static void main(String[] args) {
    SpellChecker spellChecker;
    try {
       spellChecker = new SpellChecker(args[0]);
       spellChecker.checkFile(args[1]);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

现在我的代码看起来像这样:

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

class TopSort {
    TopSort Graph = new TopSort();

    public static void topologicalSort(Graph graph) {
        Queue<TopVert> q = new LinkedList<TopVert>();
        int vertexProcessesCtr = 0;
        for (TopVert m : graph.nodes) {
            if (m.inDegree == 0) {
                ++vertexProcessesCtr;
                q.add(m);
                System.out.println(m.data);
            }
        }
        while (!q.isEmpty()) {
            TopVert m = q.poll();
            for (TopVert child : m.AdjacenctNode) {
                --child.inDegree;
                if (child.inDegree == 0) {
                    q.add(child);
                    ++vertexProcessesCtr;
                    System.out.println(child.data);
                }
            }

        }
        if (vertexProcessesCtr > graph.vertices) {
            System.out.println();
        }

    }

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        int count = 0;
        Graph g = new Graph();
        HashMap hm = new HashMap();

        // For Console input
        Scanner s = new Scanner(System.in);

        // For file input : Comment if the input is from console

        try {
            File file = new File(s.nextLine());
            s = new Scanner(file);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        while (s.hasNext()) {
            String temp = s.nextLine();
            StringTokenizer st = new StringTokenizer(temp);
            String word1 = st.nextToken();

            if (!hm.containsKey(word1)) {
                TopVert course = new TopVert(word1);
                hm.put(word1, course);
            }
            TopVert course = (TopVert) hm.get(word1);
            while (st.hasMoreTokens()) {
                String word = st.nextToken();
                if (!hm.containsKey(word)) {
                    TopVert prereq = new TopVert(word);
                    hm.put(word, prereq);
                }
                TopVert prereq = (TopVert) hm.get(word);
                prereq.AdjacenctNode.add(course);
                course.inDegree++;
            }
            g.nodes.add(course);
            count++;
        }
        g.vertices = count;
        System.out.println("The sequence of courses that one could take" +
                            "that satisy the prerequisites is:");
        topologicalSort(g);

        }
    }

class Graph {
    public int vertices;
    LinkedList<TopVert> nodes = new LinkedList<TopVert>();
}

class TopVert {
    public String data;
    public int dist;
    public int inDegree;
    LinkedList<TopVert> AdjacenctNode = new LinkedList<TopVert>( );

    public void addAdjNode(final TopVert Child){
        AdjacenctNode.add(Child);
        Child.inDegree++;
    }

    public TopVert(String data) {
        super();
        this.data = data;
    }
 }

仍然不习惯以这种方式编写代码。请协助。

0 个答案:

没有答案