It's not a secret anymore!
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Invalid Flag Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Invalid Flag Question " Watch "Invalid Flag Question " New topic

Invalid Flag Question

Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113
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:

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(..)



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 ?

Alan Morgan
Ranch Hand

Joined: Apr 18, 2005
Posts: 113

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

Joined: Oct 02, 2005
Posts: 4
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

SCJP 1.4<br />SCJD
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Invalid Flag Question
It's not a secret anymore!