简体   繁体   English

Java Print Stream打印空间

[英]Java Print Stream printing spaces

I'm trying to print out a collection of words to a CSV file and am trying to avoid spaces being printed as a word. 我正在尝试将一组单词打印到CSV文件中,并试图避免将空格打印为单词。

    static TreeMap<String,Integer> wordHash = new TreeMap<String,Integer>();
    Set words=wordHash.entrySet();
    Iterator it = words.iterator();

      while(it.hasNext()) {
        Map.Entry me = (Map.Entry)it.next();
        System.out.println(me.getKey() + " occured " + me.getValue() + " times");
        if (!me.getKey().equals(" ")) {
        ps.println(me.getKey() + "," + me.getValue());
        }
    }

Whenever I open the CSV, as well as in the console, the output is : 每当我打开CSV以及控制台时,输出都是:

        1
  10    1
   a    4
test    2

I am trying to remove that top entry of a space, I thought the statement checking if the key wasn't a space would work however it's still printing spaces. 我试图删除空间的顶部条目,我认为检查密钥是否不是空格的语句将起作用,但它仍然是打印空间。 Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Your condition will eliminate only single space keys. 您的情况将仅消除单个空格键。 If you want to eliminate any number of empty spaces, use : 如果要消除任意数量的空格,请使用:

if (!me.getKey().trim().isEmpty()) {
    ...
}

This is assuming me.getKey() can't be null. 这假设me.getKey()不能为null。

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

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