Search code examples
javaarraysfor-loopchartile

the type of the expression must be an array type but is resolved to char


    import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Sheet extends JFrame{
    private String[] line = {
    "wwwwwwffwwwwww",       
    "wwwwwwfffffffw",       
    "wwwwwwffwwwffw",       
    "wwwwwwffwwwffw",
    "wwwwwwfffffffw",
    "wwwwwwffwwwwww"        
    };
    String line1 = "wwwwwffwwwww";
            int tileX =50;
    int tileY= 50;
    public Sheet(){
        //setUndecorated(true);
        setVisible(true);
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //setResizable(false);
        setTitle("window");
    }
    public static void main(String[] args){
        Sheet frame = new Sheet();

    }
    public void paint(Graphics g){

        g.setColor(Color.black);
        g.drawRect(0,100,1000,1);
        for(int i = 0; i<line.length; i++){
            for(int f = 0; f<line[i].length(); f++){
                char line = line[f].charAt(i);

            }
        }
    }
}

Here is my code, I am trying to make a program that draws tiles according to an array of strings. In this paint method, I have 2 for loops, one to cycle through the array index for the lines, and one to cycle through the characters of the array. I get this error:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: The type of the expression must be an array type but it resolved to char

at Sheet.paint(Sheet.java:36)

Please could someone help me fix this? I have tried with no success.

Any help would be greatly appreciated


Solution

  • You're trying to redeclare the line variable. Change the variable name and you should be fine:

    char c = line[f].charAt(i);
    

    It's a fairly obscure error message, because by the time the compiler's understood that you're declaring a variable of type char with name line, when it looks at the initializer it sees line[f] and thinks that's crazy. The fact that the variable wouldn't even be assigned a value at that point is just another problem :)