Java EOFException

Associate
Joined
6 Dec 2007
Posts
2,103
Hey all, am getting the above exception in my uni project. Does it mean that it can't find the end of the text file that is being read in? And how do I sort it?

The offending code is:

while (anAccountNumber != "****" && activeAccounts < MAX_ACCOUNTS)
{
anAccountName = Text.readLine(accountsFileIn);
aBalance = Text.readDouble(accountsFileIn);

accountDetails [activeAccounts] = new BankAccount(anAccountNumber, anAccountName, aBalance);
activeAccounts++;
anAccountNumber = Text.readLine(accountsFileIn);
}

and:

public static void main ( String [] args ) throws IOException{
ATM anATM = new ATM();
anATM.bank.loadAccountDetails(ACCOUNT_FILENAME_IN);

}//main

If I'm being vague, please tell me and I will post more info. Thanks!
 
From the Java API:

EOFException - Signals that an end of file or end of stream has been reached unexpectedly during input.

You are reaching the end of the file whilst still trying to read data from it, your code should be checking that there is still data to be read otherwise the read operations will fail and throw exceptions.
 
Ok thanks. I'm assuming that I need something in or near the above while loop to deal with it. Been having a play, and I'm not sure what direction to take. Any help would be massively appreciated :)
 
Have you got the rest of the code because it's hard to tell what types some variables are and how they are initialized, it may help to have a sample of the file being read too (a couple of lines etc.).
 
There's a lot of code - too much to post here, but basically it reads in two Strings (accountName and accountNo) and a double (balance). It is then supposed to terminate when an account number consisisting of 8x asterisks are read in from the text file.

The text file is:

12345678
T Jowell
270.50

34781234
T Blair
100.00

11112222
J Bloggs
250.00

44448888
L Hamilton
1200.00

22228888
G Brown
150.00

********
 
Last edited:
}

public void loadAccountDetails( String filename ) throws IOException
{

String anAccountNumber;
String anAccountName;
double aBalance;
String textFilesPath = "H:/";
BufferedReader accountsFileIn = Text.open(textFilesPath + filename);

activeAccounts = 0;
anAccountNumber = Text.readString(accountsFileIn);
while (anAccountNumber != "****" && activeAccounts < MAX_ACCOUNTS)
{
anAccountName = Text.readLine(accountsFileIn);
aBalance = Text.readDouble(accountsFileIn);

accountDetails [activeAccounts] = new BankAccount(anAccountNumber, anAccountName, aBalance);
activeAccounts++;
anAccountNumber = Text.readLine(accountsFileIn);
}

accountsFileIn.close();
sortAccountDetails();
}//loadAccountDetails

This be the bit that reads in the file :P
 
Ok don't think its the && lol. Keep 'em coming, I'm rly stuck and any help rly is appreciated!
Okay, my last attempt. I think it is reading the file, processing the lines up and to **** but then reading until the end of the file and drops off the end.. i.e. exception.

You need to modify your code to have something like: while (( line = input.readLine()) != null){ .....
 
That is, what you are doing currently is comparing pointers to the string and correctly it is saying they are not the same "physical" string.
 
Thanks all! Case closed.

EDIT: or so I thought, now the bit of code to read in the data is giving me the EOF Exception, and the compiler blames this line:

aBalance = Text.readDouble(accountsFileIn);

Any ideas? And thanks all!
 
Last edited:
Back
Top Bottom