Java comma “,” in forEach
本问题已经有最佳答案,请猛点这里访问。
我在Java项目中打印逗号有一个小问题。在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .listener("lookAround", (Game g, String action, AbstractGameObject object, Object retVal) -> { System.out.print("You see: \t"); if (g.getItemsInRoom((Room) object).isEmpty() && g.getDoorsInRoom((Room) object).isEmpty()) System.out.print("nothing"); g.getItemsInRoom((Room) object) .stream() .forEach(item -> System.out.print(item.getName() +",")); g.getDoorsInRoom((Room) object) .stream() .forEach(door -> System.out.print(door.getName() +",")); System.out.println(" "); }) |
输出:
1 2 | You see: Black Wooden door, Brown Wooden door, |
我想要输出:
1 2 | You see: Black Wooden door, Brown Wooden door |
有没有一个简单的方法来解决这个问题?感谢您的帮助:)
我认为你不应该在那个
考虑改用string.join()或
直接使用
1 2 3 4 5 | StringJoiner j=new StringJoiner(",").setEmptyValue("nothing"); g.getItemsInRoom((Room)object).forEach(o -> j.add(o.getName())); g.getDoorsInRoom((Room)object).forEach(o -> j.add(o.getName())); System.out.println("You see: \t"+j); |