简体   繁体   English

为什么我的最后一个元素不打印?

[英]Why doesn't my last element print?

I'm on the last steps of finishing my BST and Trie in java and I've come across this problem. 我正在完成用Java完成BST和Trie的最后一步,并且遇到了这个问题。 I can't print the last element out to a file. 我无法将最后一个元素打印到文件中。 It's not the toString methods cause I've tried switching them around. 这不是toString方法,因为我尝试过切换它们。 I remember a while back one of my professors mentioned something about closing the file. 我记得前一段时间,我的一位教授提到了有关关闭文件的内容。 Something along the lines of putting an int value inside the parentheses of the close method, I'll develop on this below (not sure if I'm right thought, just something I vaguely recall) Here's a snippet of the code that I'm having trouble with. 将int值放在close方法的括号内,我将在下面进行介绍(不确定我是否正确,只是我隐约记得的内容)这是我正在编写的代码段有麻烦。

       try{
           output = new PrintWriter(inputFile);

           output.println("BST:");
           output.println(tree.toString());
           output.println("Trie:");
           output.println(trie.myToString());


           output.close();

        }
           catch(IOException e){}
        System.out.println("Goodbye");    

so if I recall correctly, the teacher said to do something like output.close(0) or the like, but it's shooting me errors. 因此,如果我没记错的话,老师说过要做类似output.close(0)之类的事情,但这却给我发了错。 Can anyone help me with this? 谁能帮我这个? Thank you. 谢谢。

catch(IOException e){}

you are swallowing the reason here. 您在这里吞下了原因。 Don't do that. 不要那样做

At least print the reason with 至少打印原因

catch(IOException e) {
  e.printStackTrace(System.err);
}

try 尝试

output.flush()

before close the output 在关闭输出之前

or try 或尝试

  output = new PrintWriter(inputFile,true);

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

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