以下是我到目前为止创建的代码。
这就是下面的程序正在读取文件,如果给定文件中的一个字段为空,则表示正在打印0
表示整数值,null
表示字符串值。
// Override all the methods of the DataManager Interface
public class StudentDataManager implements DataManager {
List<Student> list = new ArrayList<Student>();
@SuppressWarnings("null")
@Override
public List<Student> populateData(String fileName) {
Scanner s = null;
String nameAndRollNumber = "";
String numbers[] = null;
Integer roll1 = 0;
String roll = null;
String name = null;
String age = null;
String zip = null;
String street = null;
String city = null;
Integer age1 = 0;
String cpmletedata = null;
try {
s = new Scanner(new File("F:\\...\\StudentDetails.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (s.hasNext()) {
nameAndRollNumber = s.nextLine();
String[] nameAndRollNumbers = nameAndRollNumber.split(",");
try {
roll = nameAndRollNumbers[0];
roll1 = Integer.valueOf(roll);
cpmletedata = " " + roll;
name = nameAndRollNumbers[1];
if (nameAndRollNumbers[1].equals("")) {
cpmletedata = cpmletedata + " null ";
} else {
cpmletedata = cpmletedata + " " + name;
}
age = nameAndRollNumbers[2];
if (nameAndRollNumbers[2].equals("")) {
cpmletedata = cpmletedata + " 0 ";
} else {
age1 = Integer.valueOf(age);
cpmletedata = cpmletedata + " " + age1;
}
street = nameAndRollNumbers[3];
if (nameAndRollNumbers[3].equals("")) {
cpmletedata = cpmletedata + " null ";
} else {
cpmletedata = cpmletedata + " " + street;
}
city = nameAndRollNumbers[4];
if (nameAndRollNumbers[4].equals("")) {
// System.out.print("null");
cpmletedata = cpmletedata + " null ";
} else {
// System.out.print(city + " ");
cpmletedata = cpmletedata + " " + city;
}
zip = nameAndRollNumbers[5];
if (nameAndRollNumbers[5].equals(" ")) {
cpmletedata = cpmletedata + " null ";
} else {
cpmletedata = cpmletedata + " " + zip;
}
Address a1 = new Address(street, city, zip);
cpmletedata = cpmletedata + " ";
System.out.println(cpmletedata);
list.add(new Student(roll1, name, age1, a1));
}
catch (Exception e) {
e.printStackTrace();
}
}
s.close();
return list;
}
}
现在我有这么多代码,它提供了以下输出:
1 Arjun 12 GhandiNagar Pune 411020
2 Deep 8 M.G.Road Mumbai 411032
3 Deep 3 F.C.Road Pune 411032
4 Neha 10 Camp Pune 411023
5 Seema 0 null null null
6 Bani 6 Camp Mumbai 411045
7 Deepali 3 M.G.Road Mumbai 411032
8 Neha 0 null Mumbai null
9 Meena 9 Nariman Point Mumbai 411045
10 Chaitali 3 M.G.Road Mumbai 411032
11 Bani 6 Camp Mumbai 411045
12 Deepali 3 M.G.Road Mumbai 411032
13 Neha 10 F.C. Road Pune 411023
14 Deep 3 F.C.Road Pune 411032
15 Beena 6 Camp Mumbai 411045
另一个功能是验证数据。即,如果记录包含0
或null
,我们必须使其无效,即我们必须删除该特定行并将异常抛出为InsufficientDataException
。我已经创建了那个例外,但我没有得到它。如何再次阅读该列表并检查行是否包含0
或null
并验证它?
答案 0 :(得分:1)
一种可能的解决方案,我的建议是使用org.apache.commons
中的List<Student> lAllStudent = lStudentDataManager.populateData("testfile.txt");
int lOldSize = lAllStudent.size();
CollectionUtils.filter(lAllStudent, new ListSelector<Student>() {
@Override
public boolean select(int pIndex, Student pInput) {
if (pInput.getAge()==0 || pInput.getZip() == null || pInput.getStreet() == null || pInput.getCity() == null){
return false;
}
return true;
}
});
if (lOldSize != lAllStudent.size()){
throw new InsufficientDataException();
}
:
select
如果false
返回filter
,则该元素将从列表中删除。
调用exception
后,您可以将列表的旧大小与新列表进行比较。如果当前尺寸和旧尺寸之间存在差异,则会移除某些元素,您可以抛出3.4.5
。
答案 1 :(得分:0)
您可以创建一个validate():boolean方法,该方法读取数组的每个条目并测试是否存在0或null值。如果它检测到这种情况,则返回false,否则在方法结束时返回true。
答案 2 :(得分:0)
如果您修改当前的StudentList,那么它将抛出异常, 因此创建另一个studentList,条件是只选择所需的 条目将是一个很好的方法,像这样:
public void validateData(List<Student> studentList)
throws InsufficientDataException {
List<Student> studentList2 = new ArrayList<Student>();
// studentList2.addAll(studentList);
int i = 0;
for (Student s : studentList) {
i++;
if ((s.getRollno() == 0) || (s.getStudentName() == "null")
|| (s.getAge() == 0)
|| (s.getAddress().getStreetName() == "null")
|| (s.getAddress().getCity() == "null")
|| (s.getAddress().getZipCode() == "null")) {
} else {
studentList2.add(s);
}
}
for (Student s : studentList2) {
System.out.println(s.getRollno() + " " + s.getStudentName() + " "
+ s.getAge() + " " + s.getAddress().getStreetName() + " "
+ s.getAddress().getCity() + " "
+ s.getAddress().getZipCode());
}
}
所以这是你想要的输出(过滤):
1 Arjun 12 GhandiNagar Pune 411020
2 Deep 8 M.G.Road Mumbai 411032
3 Deep 3 F.C.Road Pune 411032
4 Neha 10 Camp Pune 411023
6 Bani 6 Camp Mumbai 411045
7 Deepali 3 M.G.Road Mumbai 411032
9 Meena 9 Nariman Point Mumbai 411045
10 Chaitali 3 M.G.Road Mumbai 411032
11 Bani 6 Camp Mumbai 411045
12 Deepali 3 M.G.Road Mumbai 411032
13 Neha 10 F.C. Road Pune 411023
14 Deep 3 F.C.Road Pune 411032
15 Beena 6 Camp Mumbai 411045