I am writing a program that reads a DAT file with names. The names are stored in an ArrayList of Type Name, the Name object can hold the first, middle and last name. Once the names are stored in the ArrayList they are then sorted alphabetically by last name and written back to the file. I wanted to know what I can do to improve efficiency or eliminate redundant code. Note: This Program does work.
Name.java
public class Name
{
private String firstName;
private String midName;
private String lastName;
public Name(String firstName, String lastName)
{
this.firstName=firstName;
this.lastName=lastName;
this.midName="";
}
public Name(String firstName, String midName, String lastName)
{
this.firstName=firstName;
this.midName=midName;
this.lastName=lastName;
}
public String getFirstName()
{
return firstName;
}
public String getMidName()
{
return midName;
}
public String getLastName()
{
return lastName;
}
public String toString()
{
if(midName.equals(""))
{
return "First Name: " + getFirstName()
+ "\nLast Name: " + getLastName();
}
return "First Name: " + getFirstName()
+ "\nMiddle Name: " + getMidName()
+ "\nLast Name: " + getLastName();
}
}
Database.java
public class Database
{
public static final String FileName="Name Database.dat";
private ArrayList <Name> name;
public Database()
{
name=new ArrayList<Name>();
getData();
}
public void getData()
{
BufferedReader in=null;
StringTokenizer tokenizer=null;
String fullName, firstName, midName, lastName;
fullName=firstName=midName=lastName="";
try{
in=new BufferedReader(new FileReader(FileName));
if(in.ready())
{
while((fullName=in.readLine())!=null)
{
firstName=midName=lastName="";
tokenizer=new StringTokenizer(fullName);
if(tokenizer.hasMoreTokens())
{
firstName=tokenizer.nextToken();
}
if(tokenizer.hasMoreTokens())
{
midName=tokenizer.nextToken();
}
if(tokenizer.hasMoreTokens())
{
lastName=tokenizer.nextToken();
name.add(new Name(firstName, midName, lastName));
}else{
name.add(new Name(firstName, midName));
}
}
}
}
catch(Exception e){
System.out.println(e.getCause());
System.exit(1);
}
finally{
try{
in.close();
}
catch(IOException e){
System.out.println(e.getCause());
System.exit(1);
}
}
}
public void sortByLastName()
{
ArrayList <Name> newName=new ArrayList<Name>();
String word=name.get(0).getLastName();
int min;
int pos;
while(!name.isEmpty())
{
word=name.get(0).getLastName();
min=0;
pos=0;
for(int i=0; i<name.size(); i++)
{
if(word.compareTo(name.get(i).getLastName())<min)
{
word=name.get(i).getLastName();
min=word.compareTo(name.get(i).getLastName());
pos=i;
}
}
newName.add(0, name.remove(pos));
}
name=newName;
}
public void sendToFile()
{
BufferedWriter out=null;
try{
out=new BufferedWriter(new FileWriter(FileName));
clearDataFile();
for(int i=0; i<name.size(); i++)
{
out.write(name.get(i).getFirstName() + " " + name.get(i).getMidName() + " " + name.get(i).getLastName());
out.newLine();
}
}
catch(Exception e){
System.out.println(e.getCause());
System.exit(1);
}
finally{
try{
out.close();
}
catch(IOException e){
System.out.println(e.getCause());
System.exit(1);
}
}
}
public void clearDataFile()
{
FileOutputStream out=null;
FileChannel file=null;
try{
out=new FileOutputStream(FileName);
file=out.getChannel();
file.truncate(0);
}
catch(Exception e){
System.out.println(e.getCause());
System.exit(1);
}
finally{
try{
out.close();
file.close();
}
catch(IOException e){
System.out.println(e.getCause());
System.exit(1);
}
}
}
public void printData()
{
for(int i=0; i<name.size(); i++)
{
System.out.println(name.get(i) + "\n");
}
}
}
Sample Input from File
Stephen Forsyth
Owen Hodges
Sally James
Gavin Blake
Carl Mitchell
Caroline Bower
Maria Murray
Bernadette Alsop
Ella Reid
Diane Blake
Sample Output to File
Bernadette Alsop
Diane Blake
Gavin Blake
Caroline Bower
Stephen Forsyth
Owen Hodges
Sally James
Carl Mitchell
Maria Murray
Ella Reid