Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Invalid Flag Question

 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

My requirements state the following in the data file format:

1 byte flag. 00 implies valid record, 0xFF implies deleted record

So each record has a 1 byte flag at that start of it.
I was doing something like this:

<CODE>
private final static int INVALID_FLAG = 0xFF

extract from read(..)

int flag = temp[0];
if(flag == INVALID_FLAG){
throw new RecordNotFoundException("Record number " + recNo
+ " was not found");

}

extract from delete(..)

randomAccessFile.write(this.INVALID_FLAG);


</CODE>

However upon debugging I found that an invalid (deleted) record
was returning -1.
So the check in read was returning false even if the record was deleted
If I change the invalid flag declarartion to

private final static int INVALID_FLAG = -1

then it all works ok.

Firstly am I missing here ?
And secondly is it ok to do it this way ?

Thanks.
 
Alan Morgan
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doh.

Of course the easy answer is rather than check if flag == INVALID_FLAG all I have to do is check if flag != VALID_FLAG.

And given that VALID_FLAG is set to 00 there is no problem with that.

I've been looking at this for too long
 
Tinky
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

Since the length of the flag field in your database is one byte, you should use the following to declare your flag:



The value of INVALID_FLAG is now -1 and you can now use your original if-statement to test whether a record is valid or not.

Regard, Erwin
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic