Question

I am creating one J2ME application which read/write RMS record. I able to read and write
record in RMS but now problem is that I want to delete record by accepting some value
like accountNumber.
Format of RMS record.

101,ABC,12345,12345   

and String str contain following data.

String str=accountSrNumber +","+ name +","+ balance +","+ TextDeposit;
deleteRecStore(str,accountSrNumber);

And I need to accept accountNumber(101) from user and need to delete this record.
Here is my Delete method.

 public void deleteRecStore(String str, String accNumber121) //
 {
   int s=str.indexOf(accNumber121);     
   System.out.println("index in delete function"+s);          
           if(s==0)
            {
             try{

                 rs.deleteRecord(s);
                    // RecordStore.deleteRecordStore(REC_STORE);
                     System.out.println("record delete successfully");
                 }
                catch (Exception e)
                {}   
     }
}  

I tried to use both of method rs.deleteRecord(s) and RecordStore.deleteRecordStore(REC_STORE);.
But none helps.

Était-ce utile?

La solution

You always delete record 0 which is the first record, which is a bad idea. For example, if you add two records, and delete them, and than add another record, it will be indexed as 2, so you will have to call deleteRecord(2) to remove it. Method deleteRecordStore() removes entire recordStore (which contains records) - after that, if you create one, the next added record will be indexed as zero.

If I got the idea, you want to delete a record by it's acoountNumber. If i'm right, you need to find the recordID by it's contents. The code will probably look like this (may have mistakes, did not test it, but the idea is important):

    public void deleteRecStore(String accNumber121) {
        RecordEnumeration e = rs.enumerateRecords();
        int found = -1;
        while (e.hasMoreElements()) {
            int id = e.nextRecordId();
            String next = new String(e.nextRecord());
            if (next.startsWidth(accNumber121)) {
                found = id;
            }
        }
        if (found == -1) {
            System.out.println("not found!");
        } else {
            rs.deleteRecord(found);
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top