I am new to Java. I am facing a problem to print a list of string separated by commas inside brackets.
public class House extends Property {
protected static List<String> paints;
public String paint;
public House() {
super("House", 45);
String paints = new String();
System.out.print(paints);
}
public void addPaint(String paint) {
this.paint = paint;
ArrayList<String> House = new ArrayList<String>();
House.add(" ");
public void display() {
super.display();
List<String> words = Arrays.asList(paint);
StringJoiner strJoin = new StringJoiner(", ", " { ", " }");
words.forEach((s)->strJoin.add(s));
if (paint == null || "".equals(paint)) {
System.out.print(" { }");
}
else {
System.out.print(strJoin);
}
public static void main(String[] args) {
House house1 = new House();
house1.addPaint("Red");
house1.addPaint("Blue");
house1.addPaint("Yellow");
house1.display();
}
It should be printed like this for a house with colors:
45 House { Red, Blue, Yellow }
Or like this for a house without color (empty):
45 House { }
However, the output of my code only prints the last added color:
45 House { Yellow }
Please help me. Thank you