我试图打印一个词的集合到一个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());
}
}每当我打开CSV时,以及在控制台中,输出如下:
1
10 1
a 4
test 2我试图删除一个空格的顶部条目,我认为检查键是否不是空格的语句可以工作,但是它仍然在打印空格。任何帮助都是非常感谢的。谢谢。
发布于 2016-05-04 05:57:17
你的情况只会消除单个空格键。如果要消除任何数量的空空间,请使用:
if (!me.getKey().trim().isEmpty()) {
...
}这是假设me.getKey()不能为null。
https://stackoverflow.com/questions/37019524
复制相似问题