-1

i want program that reads the information from the file items.dat, and then creates another file named availableItems.dat containing the information (barcode, quantity, price) of only the available items (having quantity more than 0) followed by the expected income from selling all those items. The expected income can be measured as the sum of quantity*price for all items ... i did that but there is EOFException Exception i dont know why ?

info in file is recorded as follows:

77777 5 6.7 .. .. to 6 items

my code is :

import java.io.*;
class items{
public static void main(String[] args) throws IOException{



File f=new File("items.dat");

FileInputStream ff=new FileInputStream(f); 

DataInputStream df=new DataInputStream(ff);

int[] barcod=new int[6];
int[] quan=new int[6];
double[] price=new double[6]; 
 int i=0;
 try{
while (i<barcod.length)
{

barcod[i]=df.readInt();
quan[i]=df.readInt();
 price[i]=df.readDouble();

i++;
}

File f2=new File("availableItems.dat");
FileOutputStream ff2=new FileOutputStream(f2);
DataOutputStream dou=new DataOutputStream(ff2);

int s=0;
double sumquan=0;
double sumpri=0;
while (s<6)
{
if (quan[s]!=0)
{
dou.writeInt(barcod[s]);
dou.writeInt(quan[s]);
dou.writeDouble(price[s]);
sumpri+=price[s];
sumquan+=quan[s];

}
s++;
}
dou.writeDouble(sumpri*sumquan);
}


catch(EOFException e){
            System.out.println(e);

        }
        finally{
             df.close();  
        }
    }
} 
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
swa
  • 9
  • 4

1 Answers1

0

You are reading too many bytes from the file in each loop iteration. By time you get to the 6th item, you don't have enough bytes left in the file to read.

Re-check the format of the file. Given your code, it should be 96 bytes long (2 ints * 4 bytes + 1 double * 8 bytes = 16 bytes * 6 items). Opening the file in a hex editor will help.

You might need to use readFloat() instead; that only reads 4 bytes from the file instead of 8.

Brian
  • 3,850
  • 3
  • 21
  • 37
  • i solved problem of Exception but how to check if the file is containing the information (barcode, quantity, price) of only the available items (having quantity more than 0) ? – swa Apr 10 '15 at 21:31
  • this items.dat «í,@!ÎÕÛ@≥≈@\ÁT@4Ę,d@P\(ı¬èè√_@ôôôôôö – swa Apr 10 '15 at 21:34
  • and this availableItems.dat «í,@!≥≈@\ÁT@4Äè√_@ôôôôôö@ÑÂôôôôô – swa Apr 10 '15 at 21:35
  • when i print (value of s) ..print (6) ..and the correct is 4 how to do this ? – swa Apr 10 '15 at 21:37
  • @reem, post a new question if you still need help on checking the file. – Brian Apr 14 '15 at 15:45