• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Invalid Flag Question

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic