The Artima Developer Community
Sponsored Link

Java Answers Forum
using an index file to print out specific records

11 replies on 1 page. Most recent reply: Oct 31, 2003 1:07 AM by james

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 11 replies on 1 page
james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

using an index file to print out specific records Posted: Oct 30, 2003 4:38 PM
Reply to this message Reply
Advertisement
Hi im trying to print out some records from a raf file firstly in the order they are in, in the file then in order that they are according to the index file and Im pretty sure I have the right code. IT prints them out for the first part fine but when it comes to print them out according to the index file it says

Exception in thread "main" java.io.EOFException
at java.io.RandomAccessFile.readFully(RandomAccessFile.java:361)
at java.io.DataInputStream.readUTF(DataInputStream.java:580)
at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:857)
at ShowDatabase.main(ShowDatabase.java:50)
line 50 is System.out.println(raf.readUTF()); down the bottom


public class ShowDatabase
{
static final int recLength = 113;

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


RandomAccessFile raf = new RandomAccessFile("drivers.dbf","rw");
int numRecs = (int) raf.length()/recLength;

for (int x = 0; x < numRecs; x++)
{
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
raf.readUTF();
raf.readChar();
System.out.println(raf.readInt());
}


File f = new File("license.ndx");
FileInputStream fIn = new FileInputStream(f);
DataInputStream dIn = new DataInputStream(fIn);

int offset[] = new int[numRecs];
int license[] = new int[numRecs];


for (int y = 0; y < numRecs; y++)
{
license[y] = dIn.readInt();
offset[y] = dIn.readInt();
}
fIn.close();

for(int z=0; z<numRecs; z++)
{

raf.seek((int)offset[z]);

System.out.println(raf.readUTF());
System.out.println(raf.readInt());
raf.readUTF();
raf.readChar();
System.out.println(raf.readInt());

}

raf.close();

}
}


Senthilnathan

Posts: 13
Nickname: ssnathan
Registered: Jul, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 5:21 PM
Reply to this message Reply
You have the for loop as follows:

for (int x = 0; x < numRecs; x++)
{
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
ra f.readUTF();
raf.readChar();
System.out.println(raf.readInt());
}

Try changing this as follows:
for (int x = 0; x < numRecs; x++)
{
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
// raf.readUTF();
//raf.readChar();
System.out.println(raf.readInt());
}

Follow this wherever you read from the file.

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 5:47 PM
Reply to this message Reply
There is parts of the file i dont want to print out, and the only way to get to the parts i want to print out is to read in the others?

Senthilnathan

Posts: 13
Nickname: ssnathan
Registered: Jul, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 6:01 PM
Reply to this message Reply
Then just increment your x value in the for loop by 2, by doing x+=2 instead of x++.

Like,
for(int x = 0; x < numRecs; x+=2)

instead of,
for(int x = 0; x < numRecs; x++)

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 6:04 PM
Reply to this message Reply
like the 1 record will contain

name string
int licensenumber
string address
char gender
int yearissued

and i only want to print out name, license number and yearissued and I cant get to year issued unless i read in the other 2 can I?

Senthilnathan

Posts: 13
Nickname: ssnathan
Registered: Jul, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 6:18 PM
Reply to this message Reply
sorry I misunderstood ur recLength variable.
I guess, the record length varies from record to record. we cannot hardcode it as 113, right.

so, one of the way to handle this is as,

<code>

boolean EOF = false;

while( !EOF ) {
try {
// your code goes here
System.out.println(raf.readUTF());
System.out.println(raf.readInt());
raf.readUTF();
raf.readChar();
System.out.println(raf.readInt());
} catch(EOFException e){
EOF = true;
}
}
</code>

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 6:51 PM
Reply to this message Reply
found one problem each record is meant to be 81 not 113. changd that but i still get the same problem.

The license index contains
licensenumber and offset

696458 162
1504503 243
1519980 567
3501227 486
3510402 81
3513671 0
3513986 405
3516037 324

might have something to do with this

Senthilnathan

Posts: 13
Nickname: ssnathan
Registered: Jul, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 6:57 PM
Reply to this message Reply
you are doing a

raf.seek((int)offset[z]);

The offset may be set beyond the end of the file.
so, try this..

if ((int)offset[z] < raf.length() )
{
raf.seek((int)offset[z]);

System.out.println(raf.readUTF());
System.out.pr intln(raf.readInt());
raf.readUTF();
raf.readChar();
System.out.println(raf.read Int());

}

The else condition depends on how you want to handle that situation.

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 7:09 PM
Reply to this message Reply
I did a printout too see where the offset went to call first

for(int z=0; z<numRecs; z++)
{
System.out.println("offset[" + z + "] = " + offset[z]);

raf.seek((int)offset[z]);

System.out.println(raf.readUTF());
System.out.println(raf.readInt());
raf.readUTF();
raf.readChar();
System.out.println(raf.readInt());


}

and it printed
offset[0] = 162

Senthilnathan

Posts: 13
Nickname: ssnathan
Registered: Jul, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 7:11 PM
Reply to this message Reply
If you dont mind, just send me ur code and a sample file ( may be with sample data, need not be original one), I can look into it.. As it is not an applicationException, but a unchecked exception should not be a problem to fix.

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 30, 2003 7:19 PM
Reply to this message Reply
email sent

james

Posts: 8
Nickname: jamezz
Registered: Oct, 2003

Re: using an index file to print out specific records Posted: Oct 31, 2003 1:07 AM
Reply to this message Reply
I figured out the problem its something to do with me trying to create an index based on firstname + " " + surname it doesnt like.

originally i was doing it like

surname[x] = seqfile.readUTF().trim();
firstname[x] = seqfile.readUTF().trim();
name[x] = firstname[x] + " " + surname[x];


and then


//writing the random access file
for(int y=0; y<numRecs;++y)
{
raf.writeUTF(name[y].concat(" ").substring(0,31) );

//31 being firstname 15 + surname 15 + 1 for spacing ?

raf.writeInt(license[y]);
raf.writeUTF(address[y].concat(" ").substring(0,35) );
raf.writeChar(gender[y]);
raf.writeInt(yearIssued[y]);
}

Flat View: This topic has 11 replies on 1 page
Topic: java will not load in yahoo games Previous Topic   Next Topic Topic: quick question on main(String[] argv)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use