// imports
import java.io.*; // Input stream
import java.util.*; // Date
//import java.text.*; // SimpleDateFormat
class FinalPrep {
//** arrays for saving
static String firstNameArray[] = new String[0];
static String lastNameArray[] = new String[0];
static float gradeArray[] = {};
public static void main(String args[]){
readInFile();
System.out.println(" Sorted By last name ");
sort(lastNameArray);
for (int i= 0; i < lastNameArray.length; i++)
{
System.out.println(firstNameArray[i] + " " + lastNameArray[i] + " "
+ gradeArray[i]);
}
System.out.println("\n\n\n Sorted By first name");
sort(firstNameArray);
for (int i= 0; i < lastNameArray.length; i++)
{
System.out.println(firstNameArray[i] + " " + lastNameArray[i] + " "
+ gradeArray[i]);
}
} //end main
//*************************** enlarge ****************************************
static private float[] enlargeArray(float[] currentArray)
{
float[] newArray = new float[currentArray.length +1];
for (int i = 0; i<currentArray.length; i++)
newArray[i] = currentArray[i];
return newArray;
}
static private String[] enlargeArray(String[] currentArray)
{
String[] newArray = new String[currentArray.length +1];
for (int i = 0; i<currentArray.length; i++)
newArray[i] = currentArray[i];
return newArray;
}
// *********************** read in the file **************************************************
static private void readInFile()
{
String filename = "C:\\Documents and Settings\\KoKo\\My Documents\\Judy's Classes\\Java Final\\StudentGrades.dat";
// Streams for reading in data
DataInputStream input;
FileInputStream finStream = null;
// variables
String firstName,lastName;
float grade;
int totalBytes = 0;
File f1 = new File(filename); // create a file object
if ((f1.exists()) && (f1.isFile())) //check to see if the file exists and is a file
{
System.out.println("The file " + f1.getName() + " exists");
System.out.println(" ");
// try to open the input file and read all the data
try {
finStream = new FileInputStream(f1);
input = new DataInputStream(finStream);
while (finStream.available() !=0) //as long as there are more bytes to read
{
firstName =input.readUTF();
lastName =input.readUTF();
grade = input.readFloat();
// enlarge and add data to array
firstNameArray = enlargeArray(firstNameArray);
lastNameArray = enlargeArray(lastNameArray);
gradeArray = enlargeArray(gradeArray);
firstNameArray[firstNameArray.length-1] = firstName;
lastNameArray[lastNameArray.length-1] = lastName;
gradeArray[gradeArray.length-1] = grade;
} //end while
} //end try
catch (EOFException eEOF)
{ System.out.println("End of listing"); }
catch (IOException eIO) { // any other IO error?
System.out.println("Exception is :- " + eIO); }
// what do I want to do no matter what.
finally
{
System.out.println(" ");
try //attempt to close the file
{ finStream.close(); }
catch (IOException e)
{ System.out.print("Exception is :- " + e); }
} // end finally
} //end true portion of if
else
{
System.out.print ("The file " + f1.getName() + " does not exist.");
System.exit(1);
}
}//end readInFile
//***************************** sort routines *********************************************
//method to sort arrays
private static void sort(String tempArray[])
{
// loop to control number of passes
for (int pass = 0; pass < tempArray.length-1; pass++)
{
for (int element = pass; element < tempArray.length; element++)
if (tempArray[pass].compareTo(tempArray[element])>0)
{
swap(firstNameArray, pass, element);
swap(lastNameArray, pass, element);
swap(gradeArray, pass, element);
}
}
}
private static void sort(float tempArray[])
{
// loop to control number of passes
for (int pass = 0; pass < tempArray.length-1; pass++)
{
for (int element = pass; element < tempArray.length; element++)
if (tempArray[pass] > tempArray[element])
{
swap(firstNameArray, pass, element);
swap(lastNameArray, pass, element);
swap(gradeArray, pass, element);
}
}
}
// method to swap two elements of an array
private static void swap(String swapArray[], int first, int second)
{
String hold; // temporary holding area for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}
private static void swap(float swapArray[], int first, int second)
{
float hold; // temporary holding area for swap
hold = swapArray[first];
swapArray[first] = swapArray[second];
swapArray[second] = hold;
}
} //end class
This is what is in the data file:
James Wood 94
Meshell Doan 100
Yoli Crespo 87
Samuel Beck 51
Polo Koren 82
Dylan Bunson 64
Michael Kore 99
Mya Young 100
Annie Lee 89
Wayne Smith 79