import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException
{
Scanner in = new Scanner( System.in );
// StudentInfo temp storage
int studentCount, year;
String last, first, email, major;
double gpa;
// CourseInfo temp storage
int courseCount;
String course, days, times;
// Prompt for input file path
System.out.print( "Enter the input file path: " );
String inputFileName = in.next();
File inputFile = new File( inputFileName );
Scanner inFile = new Scanner( inputFile );
// Prompt for output file path
System.out.println( "Enter the output file path: " );
String outputFileName = in.next();
PrintWriter outputFile = new PrintWriter( outputFileName );
// Read first line to determine number of students and create array.
studentCount = inFile.nextInt();
StudentInfo[] students = new StudentInfo[studentCount];
// Main loop to read in input file
for ( int i = 0; i < studentCount; i++ )
{
last = inFile.next();
first = inFile.next();
email = inFile.next();
year = inFile.nextInt();
major = inFile.next();
gpa = inFile.nextDouble();
courseCount = inFile.nextInt();
students = new StudentInfo( last, first, email, year, major, gpa, courseCount );
for ( int j = 0; j < courseCount; j++ )
{
inFile.nextLine(); // Clear buffer
course = inFile.nextLine();
days = inFile.next();
times = inFile.next();
CourseInfo newCourse = new CourseInfo( course, days, times );
students.setCoursesTaken( newCourse, j );
}
inFile.nextLine(); // Clear buffer
students.setDaysAvailable( inFile.nextLine() );
}
// Sort courses
StudentInfo temp = new StudentInfo();
boolean swap = false;
while ( swap == true )
{
swap = false;
for ( int i = 0; i < students.length; i++ )
{
if ( students.getGpa() < students[i+1].getGpa() )
{
temp = students;
students = students[i+1];
students[i+1] = temp;
swap = true;
}
}
}
// Output Problems Here!!!!!!!
for ( int i = 0; i < students.length; i++ )
{
outputFile.println( students.getLastName() );
outputFile.println( students.getFirstName() );
}
// Close input and output files
}
}
Basically I have a file of student info I'm reading in and that works just fine. My problem is outputting the sorted data back to a file selected by the user. In the output section nothing at all happens. If the file isn't present it gets created correctly, but I can't seem to get anything to write to the txt file at all. I can't figure out what I've done wrong. Any help would be greatly appreciated.
Thanks,
Runicode
EDIT: Wow, what the hell. After posting I added:
inFile.close();
outputFile.close();
And now it works fine. What the heck happened. Why must the files be closed for this to work?