Search code examples
javaarraysstringimportstringbuilder

Java error "Array required, but String found" while importing text file


I'm trying to import a tab delimited text file into an 2D array but when trying to assign the values of the splitted String into the array I get the error "Array required, but String found".

Here is my code so far:

try {
    FileReader fr = new FileReader ("Laberinto.txt");
    BufferedReader br = new BufferedReader(fr);
    
    String s,str;
    String[] buffer;
    
    int y=0;
    
    while ((s=br.readLine())!= null){
        StringBuilder builder = new StringBuilder();
        str=builder.append(s).toString();
        buffer=str.split("\t");
        
        for (int x=0;x<str.length();x++){
            this.lab[x][y]=Integer.parseInt(str[x]);
        }

        y++;
    }
}

I get the error on the line this.lab[x][y]=Integer.parseInt(str[x]);.

The file is basically a bunch of 1s and 0s that form a labyrinth (1 being the walls and 0 the corridors) delimited by tabulator.

What am I doing wrong?


Solution

  • str is a string - a line from your input... That's not what you wanted.

    You need to parse your buffer:

    for (int x=0;x<buffer.length();x++){
         this.lab[x][y]=Integer.parseInt(buffer[x]);
    }