Monday, 25 September 2017

Java comma "," in forEach

I have got a little problem with printing out comma in my java project. In forEach section the program is always printing out "," > even after the last word... see output




.listener("lookAround", (Game g, String action, AbstractGameObject object, Object retVal) -> {
System.out.print("You see: \n\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("\n");
})


The output:




You see:    
Black Wooden door, Brown Wooden door,


I would like output:



You see:    
Black Wooden door, Brown Wooden door



Is there a easy way to solve this problem ?
Thank you for a help :)

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...