[java] Java Practice Problem

Started by
2 comments, last by CaptainJester 12 years, 12 months ago
Hi all, I'm continuing my Java studies before I jump into working on a game and ran into an issue that I just can't seem to solve. I have no idea what's wrong with this code:


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?
Advertisement

My problem is outputting the sorted data back to a file...

File inputFile = new File( inputFileName );
...
PrintWriter outputFile = new PrintWriter( outputFileName );


You're reading from a File but writing to a PrintWriter... Shouldn't you be writing to a File?

Edit: Sorry! Typed before thinking - duh!
Yes, you need to close the file or outputstream to commit data.
Stickmen Wars 2 is in development.
Meanwhile try Bloodridge

And now it works fine. What the heck happened. Why must the files be closed for this to work?


For computers, disk I/O is considered to be very slow. (For lots of reasons I won't go into).

So to avoid any unnecessary slow downs, data to be written to a file can be bufferred and then written in 1 go.

This is what's happening in your program. Even though some data has be sent through an output stream, it hasn't actually been written to disk.
The buffer will get written periodically, but if we want to force it happen use Flush.

Also this happens automatically when you close the file.

And now it works fine. What the heck happened. Why must the files be closed for this to work?


File writing is buffered to a certain extent and if you don't close the file the buffer doesn't always get flushed properly.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement