我正在尝试创建一个文件阅读器。读者将返回一个对象数组。但是,[if(a =='');]语句总是传递为true。在程序读取的文件中,第一个字符是'P',但if语句仍然返回true。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class MyFileReader
{
Contact class1 = new Contact();
public ArrayList<Contact> read(File file) throws IOException
{
ArrayList<Contact> list = new ArrayList<Contact>();
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
String line;
int x = 0;
int i = 0;
int start = 0;
int end;
int count = 1;
while ((line = br.readLine()) != null)
{
while (i < line.length())
{
char a = line.charAt(i);
if (a == ' ');
{
end = i;
System.out.println(count);
if (count == 1)
{
class1.setFirstName(line.substring(start, end + 1));
//System.out.println(line.substring(start, end + 1));
start = end;
count++;
}
else if (count == 2)
{
class1.setLastName(line.substring(start, end + 1));
//System.out.println(line.substring(start, end + 1));
start = end;
count++;
}
else if (count == 3)
{
class1.setPhone(line.substring(start, end + 1));
//System.out.println(line.substring(start, end + 1));
start = end;
count++;
}
else if (count == 4)
{
class1.setEmail(line.substring(start, end + 1));
//System.out.println(line.substring(start, end + 1));
start = end;
count++;
list.add(class1);
}
}
i++;
class1 = new Contact();
x++;
count++;
}
x = 0;
count = 0;
}
br.close();
return list; // returns the array
}
}
答案 0 :(得分:6)
删除分号。 if (a == ' ')
。
同时建议你删除这个问题,因为它只是一个错字。