[英]Reading text file and copying text into array in Java
I'm still really new to Java, and I've been at this issue for a while now.我对 Java 还是很陌生,而且我在这个问题上已经有一段时间了。 I've got a text file like this:
我有一个这样的文本文件:
city name,x,y
Beijing
367
661
London
375
625
Washington
802
737
so as the first line states, after each city name there's an x and y value and what I'm trying to do is to read each line of the file and take in the x and y value.所以正如第一行所说,在每个城市名称之后都有一个 x 和 y 值,我要做的是读取文件的每一行并获取 x 和 y 值。 Right now I've got a reader method i can use:
现在我有一个可以使用的阅读器方法:
public class MyFileReader {
public MyFileReader(String fileName) {
try {
in = new BufferedReader(new FileReader(fileName));
buffer = in.readLine(); // Store first line
}
catch (IOException e) {
System.out.println("Cannot read file \'"+fileName+"\'");
System.exit(0);
}
}}
public String readString() {
String line = buffer;
if (buffer == null) {
System.out.println("Error. The end of file was reached, so another string cannot be read from it");
return null;
}
try {
buffer = in.readLine();
}
catch (IOException e) {
System.out.println("Error. Cannot read from file");
System.exit(0);
}
return line;
}
Here is my current code:这是我当前的代码:
public Program(String filename, Boolean b){
cityArray = new City[3];
//the following line is the line I feel has an issue
MyFileReader filereader = new MyFileReader(filename);
String line;
int i = 0;
while((line=filereader.readString())!=null)
{
String[] ar =line.split("\n");
City arr = new City(ar[0], Integer.parseInt(ar[1]) , Integer.parseInt(ar[2]));
cityArray[i++] = arr;
}
if (b){
Map citymap = new Map();
for (City city : cityArray){
citymap.addCity(city);
}}}
Sorry for the long question, but basically, the MyfileReader can't seem to read the text properly and keeps outputting: "Error. Cannot read from file", which seems to mean IOError.很抱歉这个问题很长,但基本上,MyfileReader 似乎无法正确读取文本并不断输出:“错误。无法从文件中读取”,这似乎意味着 IOError。 With my limited experience, errors usually point out where in your code you screwed up and sometimes how, but in this case I really have no idea.
以我有限的经验,错误通常会指出你在代码中的哪些地方搞砸了,有时是如何搞砸的,但在这种情况下,我真的不知道。 (I'm eventually supposed to add city name and x and y to cityArray)
(我最终应该将城市名称和 x 和 y 添加到 cityArray)
Your actual 'Cannot read from file' error is happening in this line: in = new BufferedReader(new FileReader(fileName));
您实际的“无法从文件中读取”错误发生在这一行:
in = new BufferedReader(new FileReader(fileName));
This is most likely because you have not provided a correct path to the file.这很可能是因为您没有提供正确的文件路径。 Test your code with a full absolute path.
使用完整的绝对路径测试您的代码。 Later change your code to use a relative path.
稍后更改您的代码以使用相对路径。
The second problem I see in this line: String[] ar =line.split("\n");
我在这一行看到的第二个问题:
String[] ar =line.split("\n");
You read one line and then you 'split' it into blocks using 'end-of-line character' as a separator.您读取一行,然后使用“行尾字符”作为分隔符将其“拆分”为块。 I assume it should be ' ' (space), if you have one city per line.
如果每行有一个城市,我认为它应该是 ' '(空格)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.