导入文本文件时出现Java错误“需要数组,但找到了字符串”

时间:2022-08-20 18:09:11

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".

我正在尝试将制表符分隔的文本文件导入到2D数组中,但是当尝试将分割的String的值分配到数组中时,我得到错误“需要数组,但找到了字符串”。

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

我在行上得到了错误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.

此外,我不知道这是否有帮助,但文件基本上是一堆1和0,形成一个迷宫(1是墙壁和0走廊)由制表机界定。

Can somebody tell me what im doing wrong please?

有人可以告诉我,我做错了吗?

Thanks in advanced!

提前致谢!

3 个解决方案

#1


1  

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

str是一个字符串 - 来自你输入的一行......那不是你想要的。

You need to parse your buffer:

你需要解析你的缓冲区:

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

#2


2  

parseInt(str[x])

You can't use array subscripts: [], to index into a String, and obviously str is a String.
For a solution you might want to consider using charAt() and then String.valueOf(char), which will convert the char back in case you need it.

你不能使用数组下标:[]来索引一个String,显然str是一个String。对于解决方案,您可能需要考虑使用charAt()然后使用String.valueOf(char),这将在需要时将char转换回来。

#3


1  

You declared str as follows:

你声明str如下:

  String s,str;

and tried to use it as follows:

并尝试使用如下:

  this.lab[x][y]=Integer.parseInt(str[x]);

The declaration declares it as a String. The usage (see the RHS of the = !!) tries to use it as an array of String.

声明将其声明为String。用法(参见= !!的RHS)尝试将其用作String数组。


Actually, I think your real error is that you are attempting to parse the wrong thing. I think it should probably be this:

实际上,我认为你真正的错误是你试图解析错误的东西。我想应该是这样的:

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

#1


1  

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

str是一个字符串 - 来自你输入的一行......那不是你想要的。

You need to parse your buffer:

你需要解析你的缓冲区:

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

#2


2  

parseInt(str[x])

You can't use array subscripts: [], to index into a String, and obviously str is a String.
For a solution you might want to consider using charAt() and then String.valueOf(char), which will convert the char back in case you need it.

你不能使用数组下标:[]来索引一个String,显然str是一个String。对于解决方案,您可能需要考虑使用charAt()然后使用String.valueOf(char),这将在需要时将char转换回来。

#3


1  

You declared str as follows:

你声明str如下:

  String s,str;

and tried to use it as follows:

并尝试使用如下:

  this.lab[x][y]=Integer.parseInt(str[x]);

The declaration declares it as a String. The usage (see the RHS of the = !!) tries to use it as an array of String.

声明将其声明为String。用法(参见= !!的RHS)尝试将其用作String数组。


Actually, I think your real error is that you are attempting to parse the wrong thing. I think it should probably be this:

实际上,我认为你真正的错误是你试图解析错误的东西。我想应该是这样的:

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