Re: Project5Read



On Wed, 8 Jul 2009 19:38:12 -0700 (PDT), matt <mweppler@xxxxxxxxx>
wrote:

I am taking a Java Programming Course and running into an issue with a
project. The program for this project will use the file created in
Project5Write to create a formatted report that displays a list of the
information in the file you’ve created. Include a line at the bottom
that lists the total number of records in the file. Call this program
Project5Read. Include a test to return an error if the filename
selected doesn’t exist.

The output is:
Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.

Im pretty sure the problem is in my while loop but cant figure out how
to resolve it. When I remove the while loop the program reads the
first input but not the second.

Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.
The Employee's Number is: 1
The Employee's Name is: James Smith
The Employee's Department is: Sales
The Employee has 15 Vacation Days Available
The total number of Employees is: 2

It should read:

Enter a file name:
test.dat
test.dat is a valid file.
The file is 50 bytes long.
It was last modified on 1247104709000 in Java Standard Time.
The file can be read.
The file can be written to.
The Employee's Number is: 1
The Employee's Name is: James Smith
The Employee's Department is: Sales
The Employee has 15 Vacation Days Available
The Employee's Number is: 2
The Employee's Name is: John Darme
The Employee's Department is: Security
The Employee has 20 Vacation Days Available
The total number of Employees is: 2

Any help would be great.


import java.io.*;

public class Project5Read {

public static void main(String[] args) throws IOException {

BufferedReader stdin = new BufferedReader(new InputStreamReader
(System.in));
String fileName = new String();
System.out.println("Enter a file name: ");
fileName = stdin.readLine();
File f = new File(fileName);

if (f.exists()) {
System.out.println(f + " is a valid file.");
System.out.println(" The file is " + f.length() + " bytes
long.");
System.out.println(" It was last modified on " +
f.lastModified() + " in Java Standard Time.");
if (f.canRead()) {
System.out.println(" The file can be read.");
}
if (f.canWrite()) {
System.out.println(" The file can be written to.");
}
} else {
System.out.println("File does not exist! Please check the
filename and try again!");
System.exit(1);
}


DataInputStream istream;
istream = new DataInputStream(new BufferedInputStream(new
FileInputStream(f)));
OutputStream ostream;
int c;
ostream = System.out;

try {
int counter = 1;
c = istream.read();
Here you read the first byte of the file and assign it to c. Are you
sure that you actually want to read the first byte of the file at this
point? What data was in that byte, because you do not use it anywhere
else later.

//while ((c = istream.read()) != -1) {
while (c != -1) {
You check whether c equals -1 or not.

String empName = new String();
String dept = new String();
int numVacaDays;
empName = istream.readUTF();
I suspect that this is the read where your program is failing.

dept = istream.readUTF();
numVacaDays = istream.readInt();
System.out.println("The Employee's Number is: " +
counter);
System.out.println("The Employee's Name is: " + empName);
System.out.println("The Employee's Department is: " +
dept);
System.out.println("The Employee has " + numVacaDays + "
Vacation Days Available");
++counter;
}
You have reached the end of your while loop and you have not changed
the value of c. Your loop will continue to check the value of the
first byte of the file held in c. At some point one of your reads
will throw an EoF exception, presumably when it tries to read the
start of the non-existent third employee.

I suspect that you are not seeing all the output for the second
employee because System.out is buffered, and anything sitting in the
buffer may be lost when an exception is thrown.

System.out.println("The total number of Employees is: " +
counter);
istream.close();
ostream.close();
These two are better in a finally clause; that way the files get
closed whether or not an exception is thrown.

} catch (EOFException e) {
Now you know why not to silently eat exceptions. At the very least
print the exception message here to let yourself know what went wrong:

System.err.println(e.getMessage());

Note that I am using System.err, not System.out System.err is not
buffered so it has more of a chance of appearing on screen when things
are failing than the buffered System.out

You might also try

System.out.flush()

here to see what was in the print buffer at the time of the crash,
though that does not always work.

//closeFile();
}
}
}

.



Relevant Pages

  • Accrual, take II
    ... I'm working on accruing vacation time. ... hourly and salaried workers have different accruals (salaried depending on ... the levels, an employee table with their exemption type, ... changes you only have to change the data, not the query or other code. ...
    (microsoft.public.access.queries)
  • Re: Casting Byte Buffer returned from CeReadRecordPropsEx to CEPROPVAL in C#
    ... as a buffer to see if that makes any difference. ... string lpwstr; // Unicode string pointer ... IntPtr ptrBuf = new IntPtr; ... It's in the conversion to the structure it throws the exception. ...
    (microsoft.public.pocketpc.developer)
  • RE: Accrual, take II
    ... Employee A starting working at level one. ... vacation time for every 50 hours worked, which after the first 4,000 hours ... and she should accrue 2 hours for every 50 hours worked. ... and the new date of employment would start off the new accruals. ...
    (microsoft.public.access.queries)
  • Re: Some questions about handling exceptions and when to throw them
    ... In the event that a customer can't be ... Possibly even my own exception, ... because the caller should have checked first. ... If GetEmployeeInfo fails, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Some questions about handling exceptions and when to throw them
    ... So in those cases throwing an exception would be a good decision. ... because the caller should have checked first. ... PrinterInfo GetEmployeeDefaultPrinter ... If GetEmployeeInfo fails, ...
    (microsoft.public.dotnet.languages.csharp)