主方法没有输出?

时间:2015-12-08 04:39:43

标签: java main

我正在编写一个应该输出表的程序,但是没有输出。我没有得到任何错误 - 我的IDE中没有编译错误或来自终端或交互的运行时错误。这是我的完整主要方法:

public static void main(String[] args) throws IOException{
  if(args.length < 1){
    Scanner input = new Scanner(System.in);
    String s = input.nextLine();
    Scanner scan = new Scanner(s);
    Map<Character,Double> freq = new TreeMap<Character,Double>();
    while(scan.hasNextLine()){
      int i = Integer.parseInt(scan.next());
      char c = (char)i;
      double d = Double.parseDouble(scan.next());
      freq.put(c,d);
    }
    input.close();
    scan.close();

    HuffmanCode huff = new HuffmanCode(freq);
    for(Map.Entry<Character,String> entry : huff.codeMap.entrySet()){
     System.out.format("%3c%4d%30s", entry.getKey(), (int)entry.getKey(), entry.getValue());
    }
  }else{
    Scanner scan = new Scanner(new File(args[0]));
    Map<Character,Double> freq = new TreeMap<Character,Double>();
    while(scan.hasNext()){
      int i = Integer.parseInt(scan.next());
      char c = (char)i;
      double d = Double.parseDouble(scan.next());
      freq.put(c,d);
    }
    HuffmanCode huff = new HuffmanCode(freq);
    for(Map.Entry<Character,String> entry : huff.codeMap.entrySet()){
     System.out.format("%3c%4d%30s", entry.getKey(), (int)entry.getKey(), entry.getValue());
    }
  }      
}

我很困惑,因为程序完成,没有给我任何错误,但没有打印任何东西。有什么明显的东西我不见了吗?

编辑:这是我的HuffmanCode构造函数:

HuffmanCode(Map<Character,Double> freqMap){
   this.freqMap = freqMap;
   PriorityQueue<TreeNode> queue = new PriorityQueue<TreeNode>(new NodeComparator());
   for(Map.Entry<Character,Double> entry : freqMap.entrySet()){
     TreeNode temp = new TreeNode(entry.getKey(),entry.getValue());
     queue.add(temp);
   }
   while(queue.size() > 1){
     TreeNode t1 = queue.remove();
     TreeNode t2 = queue.remove();
     TreeNode node = new TreeNode(t1.freq + t2.freq, t1, t2);
     queue.add(node);
   }
   root = queue.remove();       
   String s = "";
   codeMap = traversal(root,s);       
}

另外,我输入一个文件作为参数,因此不涉及main方法中的第一个if块 - 只是为了完整性而添加它。

0 个答案:

没有答案