Roberto Vasquez Roberto Vasquez - 5 months ago 20
Java Question

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 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]);


Also, I don't know if this helps at all but 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.

Can somebody tell me what im doing wrong please?

Thanks in advanced!

Answer

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]);
}
Comments