In: Computer Science
Binary files are convenient to computers because they’re easily machine readable. Text files are human readable, but require parsing and conversion to process in the computer. Consider a class Person that has the following structure:
public class Student { private String name; private int age; private double gpa; public Student() { } public Student(String name, int age, double gpa) { this.name = name; this.age = age; this.gpa = gpa; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public double getGpa() { return this.gpa; } public void setGpa(double gpa) { this.gpa = gpa; } public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Student{"); builder.append(String.format("%s=%s", "name", name)); builder.append(String.format(",%s=%d", "age", age)); builder.append(String.format(",%s=%.2f", "gpa", gpa)); builder.append("}"); return builder.toString(); } @Override public boolean equals(Object obj) { if (obj != null && obj instanceof Student) { return equals((Student)obj); } return false; } public boolean equals(Student other) { if (other == null) { return false; } return name.equals(other.name) && age == other.age && Math.abs(gpa - other.gpa) < 0.0001; } }
A number of student records were written out to a file using the toString() method given above. Roughly, they look like the following:
Student{name=Joe O'Sullivan,age=22,gpa=3.14} Student(name=Jane Robinson,age=19,gpa=3.81} .... Student{name=Jill Gall,age=21,gpa=2.98}
Your task is to implement a method that will return an array of Students based on a parameter file name that is passed into the method. You will open the text file, read lines, split lines into fields, and then call the setters based on the key/value pairs in the string. Fields will always be in the same order: name, age, gpa. The array should be exactly the same size as the number of lines in the file you are reading. At most, there will be 20 lines in the file. You should not throw any exceptions from the function. if the file doesn't exist, return a zero-length array.
Some hints:
/////////////////////////////////////////////////
public class Student {
private String name;
private int age;
private double gpa;
public Student() {
}
public Student(String name, int age, double gpa) {
this.name = name;
this.age = age;
this.gpa = gpa;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public double getGpa() {
return this.gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student{");
builder.append(String.format("%s=%s", "name", name));
builder.append(String.format(",%s=%d", "age", age));
builder.append(String.format(",%s=%.2f", "gpa", gpa));
builder.append("}");
return builder.toString();
}
@Override
public boolean equals(Object obj) {
if (obj != null && obj instanceof Student) {
return equals((Student)obj);
}
return false;
}
public boolean equals(Student other) {
if (other == null) {
return false;
}
return name.equals(other.name) &&
age == other.age &&
Math.abs(gpa - other.gpa) < 0.0001;
}
}
/////////////////////////////////////////
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class StudentTest {
public static Student [] studentList(String
filename){
// first get the number of valid
lines in this file
int index=0;// to store number of
lines
BufferedReader
br1=null;
String line="";
try {
// read
file
br1=new
BufferedReader(new FileReader(filename));
// read each
file
while((line=br1.readLine())!=null){
String [] spltreslt=line.split(",");
// if line is valid , then increase index
if(spltreslt.length==3){
index++;
}
}
br1.close();
} catch (IOException e) {}
// if valid line exist
// the create array and load data
in array
if(index>0){
Student []
students=new Student[index];
index=0;
BufferedReader
br=null;
try {
br=new BufferedReader(new
FileReader(filename));
// read line by line from file
while((line=br.readLine())!=null){
// split line
String []
spltreslt=line.split(",");
// if line is valid
if(spltreslt.length==3){
// create
new student object
Student
stdnt=new Student();
// split
each and set data to student object
stdnt.setName(spltreslt[0].split("=")[1]);
stdnt.setAge(Integer.parseInt(spltreslt[1].split("=")[1]));
//
gpa=2.98}
// split
by "=" [ after spiting 2nd string hold 2.98} ]
//
represented by split("=")[1]
// 2.98} ,
this string split again by "}"
// [ after
spiting 1st string hold 2.98 ]
//
represented by split("}")[0]
stdnt.setGpa(Double.parseDouble(spltreslt[2].split("=")[1].split("}")[0]));
// add in
student array
students[index++]=stdnt;
}
}
br.close();
} catch
(IOException e) {}
// return
array
return
students;
}
else{
// return array
of length 0
return
null;
}
}
public static void main(String [] args){
Scanner sc=new
Scanner(System.in);
// take file name from user
System.out.print("Enter the file
name : ");
String filename=sc.next();
sc.close();
// load data from file and save in
array
Student []
students=studentList(filename);
// display each student data
for(int
i=0;i<students.length;i++){
System.out.println(students[i]);
}
}
}