Changing the elements in an array using [index]


import org.code.neighborhood.Painter;

public class MyNeighborhood {
  public static void main(String[] args) {

String[] paintColors = new String[3];
    
    String[] paintColors = {"Red", "White", "Blue"};
   ArrayPainter myArrayPainter = new ArrayPainter(5, 0, "south", 18, paintColors);
    myArrayPainter.moveAndPaint();

  }
}
In the ArrayPainter subclass:
public class ArrayPainter extends Painter {

  private String[] paintColors;

  public ArrayPainter(int x, int y, String direction, int paint, String[] paintColors) {
    super(x, y, direction, paint);
    this.paintColors = paintColors;
  }
  public String[] getPaintColors() {
    return paintColors;
  }

  public void moveAndPaint() {
   int index = 0;
    while(index < paintColors.length) {
      move();
      paint(paintColors[index]);
      index++;
    }
    
  }
  public void setColor(int index, String newColor) {
  return paintColors[index] = newColor;
    
  }
  
}

When I try to run my code, the error is within this line:
public void setColor(int index, String newColor) {
return paintColors[index] = newColor;

The goal of this method is to change one or more colors in the array.

Please post your actual code instead of a picture.

1 Like

Changed it. Thanks for letting me know; it’s my first day on here.

1 Like

can you be more explicit and show us the text of the error?

also please confirm the language of this code? (looks like java to me)

your function declares a void return but then tries to return a value?
this may cause an error.

also noticed that you are declaring paintColors twice (the first one says it is a string array and the second one creates an array of three string literals)

1 Like