In: Computer Science
create your own function that wraps it with try catch. Pass in the string to parse, and the name of the field, put some logging in the catch
How do I do this? say with this code segment?
void readStudents() {
//Scanner class object declare
Scanner readStudentFile = null;
//try block begin
try {
//open file
readStudentFile = new Scanner(new
File(".\\src\\test\\student.txt"));
//loop until end of file
while(readStudentFile.hasNextLine()) {
String stu = readStudentFile.nextLine();
String[] eachStu;
eachStu = stu.split(" ");
students.add(new Student(eachStu[0], eachStu[1],
eachStu[2], Long.parseLong(eachStu[3]),
Integer.parseInt(eachStu[4]), Integer.parseInt(eachStu[5]),
Integer.parseInt(eachStu[6])));
} //end while
}//end try
//catch block for file not found exception
catch(FileNotFoundException fe) {
System.out.println("\nERROR: unable to open file.\n");
}//end of catch
//close file
readStudentFile.close();
}//end method
You can try this if you need to check perticular field
following class student,School, TestSchool are just for exmples ...
/*******************************Student.java************************/
/**
* @author
*
*/
public class Student {
private String firstName;
private String middleName;
private String lastName;
private long id;
private int subMark1;
private int subMark2;
private int subMark3;
public Student(String firstName, String middleName,
String lastName, long id, int subMark1, int subMark2,
int subMark3)
{
this.firstName =
firstName;
this.middleName = middleName;
this.lastName = lastName;
this.id = id;
this.subMark1 = subMark1;
this.subMark2 = subMark2;
this.subMark3 = subMark3;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName)
{
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getSubMark1() {
return subMark1;
}
public void setSubMark1(int subMark1) {
this.subMark1 = subMark1;
}
public int getSubMark2() {
return subMark2;
}
public void setSubMark2(int subMark2) {
this.subMark2 = subMark2;
}
public int getSubMark3() {
return subMark3;
}
public void setSubMark3(int subMark3) {
this.subMark3 = subMark3;
}
@Override
public String toString() {
return "Name: " + firstName + " " +
middleName + " " + lastName + "\nID: " + id + "\nMARKS: " +
subMark1 + " "
+ subMark2 + " " + subMark3 + "\n";
}
}
/*************************************School.java****************************/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
* @author
*
*/
public class School {
private ArrayList<Student> students;
public School() {
students = new
ArrayList<Student>();
}
void readStudents() {
// Scanner class object
declare
Scanner readStudentFile =
null;
// try block begin
try {
// open
file
readStudentFile
= new Scanner(new File("student.txt"));
// loop until
end of file
while
(readStudentFile.hasNextLine()) {
String stu = readStudentFile.nextLine();
String[] eachStu;
eachStu = stu.split(" ");
String firstName = null, middleName = null,
lastName = null;
long id = 0;
int mark1 = 0, mark2 = 0, mark3 = 0;
/*
* check for name is in correct format or
not
*/
try {
firstName = eachStu[0];
middleName =
eachStu[1];
lastName = eachStu[2];
} // catch block for invalid formatted
line
catch (NullPointerException e) {
System.out.println("Name is
not correct!");
}
// check for id
try {
id =
Long.parseLong(eachStu[3]);
} // catch block for invalid formatted
line
catch (NumberFormatException e) {
System.out.println("ID is not
correct!");
}
// check for marks
try {
mark1 =
Integer.parseInt(eachStu[4]);
mark2 =
Integer.parseInt(eachStu[5]);
mark3 =
Integer.parseInt(eachStu[6]);
} catch (NumberFormatException e) {
System.out.println("Subjects
mark is not correct");
}
students.add(new Student(firstName, middleName,
lastName, id, mark1, mark2, mark3));
} //
end while
readStudentFile.close();// close file
} // end try
// catch block for file
not found exception
catch (FileNotFoundException fe)
{
System.out.println("\nERROR: unable to open file.\n");
} // end of catch
}// end method
public void printStudents() {
for (Student student : students) {
System.out.println(student.toString());
}
}
}
/**********************************SchoolTest.java***********************/
/**
* @author
*
*/
public class TestSchool {
public static void main(String[] args) {
School school = new School();
school.readStudents();
school.printStudents();
}
}
/************************student.txt**********************/
M S Dhoni 347564748 78 98 88
Virat Kohli 4356784574 44 57 88
R S Saini D33333 43 55 66
J K Sharma 3457956 F 65 44
/**********************output******************/
ID is not correct!
Subjects mark is not correct
Name: M S Dhoni
ID: 347564748
MARKS: 78 98 88
Name: Virat Kohli
ID: 4356784574
MARKS: 44 57 88
Name: R S Saini
ID: 0
MARKS: 43 55 66
Name: J K Sharma
ID: 3457956
MARKS: 0 0 0
Please let me know if you have any doubt or modify the answer, Thanks :)